unit U_Informasi;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, cxCurrencyEdit, StdCtrls, DB, ZAbstractRODataset,
  ZAbstractDataset, ZDataset, ZConnection, ExtCtrls, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, ComCtrls, cxDBData, cxGridBandedTableView,
  cxGridDBBandedTableView, cxGridDBTableView, cxXLSExport, TeEngine,
  Series, TeeProcs, Chart, DbChart, TeeFunci, JvComponentBase, JvThread,
  WaitProgress, JvExControls, JvWaitingProgress, JvBaseDlg,
  JvProgressDialog, cxGridExportLink, JvDialogs, XPMan, JvThreadDialog;

type
  TfrmInformasi = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    cxGrid1: TcxGrid;
    cxGrid1Level1: TcxGridLevel;
    Panel1: TPanel;
    Button2: TButton;
    TabSheet3: TTabSheet;
    cxGrid2: TcxGrid;
    cxGridLevel1: TcxGridLevel;
    Panel3: TPanel;
    Button5: TButton;
    TabSheet4: TTabSheet;
    Panel2: TPanel;
    Label3: TLabel;
    Label2: TLabel;
    Label1: TLabel;
    Button3: TButton;
    cxGrid3: TcxGrid;
    cxGridLevel2: TcxGridLevel;
    Panel4: TPanel;
    Button7: TButton;
    TabSheet5: TTabSheet;
    Panel5: TPanel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Button8: TButton;
    cxGrid4: TcxGrid;
    cxGridLevel3: TcxGridLevel;
    Panel6: TPanel;
    Button10: TButton;
    TabSheet6: TTabSheet;
    Panel7: TPanel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Button11: TButton;
    cxGrid5: TcxGrid;
    cxGridLevel4: TcxGridLevel;
    Panel8: TPanel;
    Button13: TButton;
    XPManifest1: TXPManifest;
    con: TZConnection;
    qryNIKKurang: TZQuery;
    Panel11: TPanel;
    Label15: TLabel;
    Button17: TButton;
    dsNIKKurang: TDataSource;
    qryKabKot: TZQuery;
    Panel12: TPanel;
    Label18: TLabel;
    Button18: TButton;
    cbKabKot1: TComboBox;
    cbKabkot2: TComboBox;
    cbKel3: TComboBox;
    cbKec3: TComboBox;
    cbKabkot3: TComboBox;
    cbKel4: TComboBox;
    cbKec4: TComboBox;
    cbKabkot4: TComboBox;
    cbKel5: TComboBox;
    cbKec5: TComboBox;
    cbKabkot5: TComboBox;
    qryKec: TZQuery;
    qryKel: TZQuery;
    qryCek: TZQuery;
    gridTableBand: TcxGridDBBandedTableView;
    gridTableBandID_USER: TcxGridDBBandedColumn;
    gridTableBandNIK: TcxGridDBBandedColumn;
    gridTableBandNKK: TcxGridDBBandedColumn;
    gridTableBandNAMA: TcxGridDBBandedColumn;
    gridTableBandTEMPAT: TcxGridDBBandedColumn;
    gridTableBandTANGGAL: TcxGridDBBandedColumn;
    gridTableBandIBU: TcxGridDBBandedColumn;
    gridTableBandAGAMA: TcxGridDBBandedColumn;
    gridTableBandKAWIN: TcxGridDBBandedColumn;
    gridTableBandKELAMIN: TcxGridDBBandedColumn;
    gridTableBandKELAINAN: TcxGridDBBandedColumn;
    gridTableBandALAMAT: TcxGridDBBandedColumn;
    gridTableBandRW: TcxGridDBBandedColumn;
    gridTableBandRT: TcxGridDBBandedColumn;
    gridTableBandDUSUN: TcxGridDBBandedColumn;
    gridTableBandKET: TcxGridDBBandedColumn;
    gridTableBandID_TANGGAL: TcxGridDBBandedColumn;
    gridTableBandTPS: TcxGridDBBandedColumn;
    gridTableBandSTATUS1: TcxGridDBBandedColumn;
    gridTableBandSTATUS2: TcxGridDBBandedColumn;
    gridTableBandGANDA: TcxGridDBBandedColumn;
    gridTableBandpanjang_nik: TcxGridDBBandedColumn;
    dsUmurKurang: TDataSource;
    qryUmurKurang: TZQuery;
    cxGrid4DBBandedTableView1: TcxGridDBBandedTableView;
    cxGrid4DBBandedTableView1ID_USER: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1NIK: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1NKK: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1NAMA: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1TEMPAT: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1TANGGAL: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1IBU: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1AGAMA: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1KAWIN: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1KELAMIN: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1KELAINAN: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1ALAMAT: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1RW: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1RT: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1DUSUN: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1KET: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1ID_TANGGAL: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1TPS: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1STATUS1: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1STATUS2: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1GANDA: TcxGridDBBandedColumn;
    cxGrid4DBBandedTableView1age: TcxGridDBBandedColumn;
    qryBantu: TZQuery;
    dsDPTHapus: TDataSource;
    qryDPTHapus: TZQuery;
    cxGrid5DBBandedTableView1: TcxGridDBBandedTableView;
    cxGrid5DBBandedTableView1ID_USER: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1NIK: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1NKK: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1NAMA: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1TEMPAT: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1TANGGAL: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1IBU: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1AGAMA: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1KAWIN: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1KELAMIN: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1KELAINAN: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1ALAMAT: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1RW: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1RT: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1DUSUN: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1KET: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1ID_TANGGAL: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1TPS: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1STATUS1: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1STATUS2: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1GANDA: TcxGridDBBandedColumn;
    cxGrid5DBBandedTableView1IFSTATUS21MeninggalDuniaIFSTATUS22DataFiktifIFSTATUS23TNIPOLRIIFSTATUS24PindahAlamatIFSTATUS25HAPUSDATA: TcxGridDBBandedColumn;
    qryBantu2: TZQuery;
    dsRekapTPS: TDataSource;
    qryRekapTPS: TZQuery;
    qryRekapDataPemilih: TZQuery;
    dsRekapDataPemilih: TDataSource;
    Panel9: TPanel;
    Button14: TButton;
    qryRekap: TZQuery;
    dsRekap: TDataSource;
    qry1: TZQuery;
    ds1: TDataSource;
    qry2: TZQuery;
    ds2: TDataSource;
    qry3: TZQuery;
    ds3: TDataSource;
    qry4: TZQuery;
    ds4: TDataSource;
    qry5: TZQuery;
    ds5: TDataSource;
    qry6: TZQuery;
    ds6: TDataSource;
    qry7: TZQuery;
    ds7: TDataSource;
    qry8: TZQuery;
    ds8: TDataSource;
    qry9: TZQuery;
    ds9: TDataSource;
    PageControl2: TPageControl;
    TabSheet7: TTabSheet;
    DBChart1: TDBChart;
    Series1: TBarSeries;
    TeeFunction1: TAddTeeFunction;
    Series2: TBarSeries;
    Series3: TBarSeries;
    Series4: TBarSeries;
    Series5: TBarSeries;
    Series6: TBarSeries;
    Series7: TBarSeries;
    Series8: TBarSeries;
    Series9: TBarSeries;
    TabSheet8: TTabSheet;
    cxGrid6: TcxGrid;
    cxGrid6DBTableView1: TcxGridDBTableView;
    cxGrid6DBTableView1id: TcxGridDBColumn;
    cxGrid6DBTableView1kabkot: TcxGridDBColumn;
    cxGrid6DBTableView1kecamatan: TcxGridDBColumn;
    cxGrid6DBTableView1kelurahan: TcxGridDBColumn;
    cxGrid6DBTableView1tanggal: TcxGridDBColumn;
    cxGrid6DBTableView1informasi: TcxGridDBColumn;
    cxGrid6DBTableView1dp4_jml: TcxGridDBColumn;
    cxGrid6DBTableView1dp4_l: TcxGridDBColumn;
    cxGrid6DBTableView1dp4_p: TcxGridDBColumn;
    cxGrid6DBTableView1dps_jml: TcxGridDBColumn;
    cxGrid6DBTableView1dps_l: TcxGridDBColumn;
    cxGrid6DBTableView1dps_p: TcxGridDBColumn;
    cxGrid6DBTableView1dpt_jml: TcxGridDBColumn;
    cxGrid6DBTableView1dpt_l: TcxGridDBColumn;
    cxGrid6DBTableView1dpt_p: TcxGridDBColumn;
    cxGrid6Level1: TcxGridLevel;
    JvThread1: TJvThread;
    JvThread2: TJvThread;
    JvThread3: TJvThread;
    openDlg: TSaveDialog;
    cxGrid1DBBandedTableView1: TcxGridDBBandedTableView;
    cxGrid1DBBandedTableView1id: TcxGridDBBandedColumn;
    cxGrid1DBBandedTableView1kabkot: TcxGridDBBandedColumn;
    cxGrid1DBBandedTableView1kecamatan: TcxGridDBBandedColumn;
    cxGrid1DBBandedTableView1kelurahan: TcxGridDBBandedColumn;
    cxGrid1DBBandedTableView1jml_tps: TcxGridDBBandedColumn;
    cxGrid1DBBandedTableView1pemilih: TcxGridDBBandedColumn;
    cxGrid1DBBandedTableView1sudah: TcxGridDBBandedColumn;
    cxGrid1DBBandedTableView1belum: TcxGridDBBandedColumn;
    cxGrid2DBBandedTableView1: TcxGridDBBandedTableView;
    cxGrid2DBBandedTableView1id: TcxGridDBBandedColumn;
    cxGrid2DBBandedTableView1kabkot: TcxGridDBBandedColumn;
    cxGrid2DBBandedTableView1kecamatan: TcxGridDBBandedColumn;
    cxGrid2DBBandedTableView1kelurahan: TcxGridDBBandedColumn;
    cxGrid2DBBandedTableView1tps: TcxGridDBBandedColumn;
    cxGrid2DBBandedTableView1jumlah: TcxGridDBBandedColumn;
    cxGrid2DBBandedTableView1laki: TcxGridDBBandedColumn;
    cxGrid2DBBandedTableView1perempuan: TcxGridDBBandedColumn;
    qrybantuTps: TZQuery;
    TabSheet9: TTabSheet;
    Panel10: TPanel;
    Label11: TLabel;
    Label12: TLabel;
    Button1: TButton;
    cbkec6: TComboBox;
    cbkabkot6: TComboBox;
    Panel13: TPanel;
    Button4: TButton;
    qryDagan: TZQuery;
    cxGrid7: TcxGrid;
    cxGridDBBandedTableView1: TcxGridDBBandedTableView;
    cxGridDBBandedColumn1: TcxGridDBBandedColumn;
    cxGridDBBandedColumn2: TcxGridDBBandedColumn;
    cxGridDBBandedColumn3: TcxGridDBBandedColumn;
    cxGridDBBandedColumn4: TcxGridDBBandedColumn;
    cxGridDBBandedColumn5: TcxGridDBBandedColumn;
    cxGridDBBandedColumn6: TcxGridDBBandedColumn;
    cxGridDBBandedColumn7: TcxGridDBBandedColumn;
    cxGridDBBandedColumn8: TcxGridDBBandedColumn;
    cxGridDBBandedColumn9: TcxGridDBBandedColumn;
    cxGridDBBandedColumn10: TcxGridDBBandedColumn;
    cxGridDBBandedColumn11: TcxGridDBBandedColumn;
    cxGridDBBandedColumn12: TcxGridDBBandedColumn;
    cxGridDBBandedColumn13: TcxGridDBBandedColumn;
    cxGridDBBandedColumn14: TcxGridDBBandedColumn;
    cxGridDBBandedColumn15: TcxGridDBBandedColumn;
    cxGridDBBandedColumn16: TcxGridDBBandedColumn;
    cxGridDBBandedColumn17: TcxGridDBBandedColumn;
    cxGridDBBandedColumn19: TcxGridDBBandedColumn;
    cxGridDBBandedColumn20: TcxGridDBBandedColumn;
    cxGridDBBandedColumn21: TcxGridDBBandedColumn;
    cxGridLevel5: TcxGridLevel;
    dsDagan: TDataSource;
    TabSheet10: TTabSheet;
    Panel14: TPanel;
    Button6: TButton;
    WaitProgress1: TWaitProgress;
    Label13: TLabel;
    editnik: TEdit;
    editnama: TEdit;
    Label14: TLabel;
    cxGrid8: TcxGrid;
    cxGridDBBandedTableView2: TcxGridDBBandedTableView;
    cxGridDBBandedColumn18: TcxGridDBBandedColumn;
    cxGridDBBandedColumn22: TcxGridDBBandedColumn;
    cxGridDBBandedColumn23: TcxGridDBBandedColumn;
    cxGridDBBandedColumn24: TcxGridDBBandedColumn;
    cxGridDBBandedColumn25: TcxGridDBBandedColumn;
    cxGridDBBandedColumn26: TcxGridDBBandedColumn;
    cxGridDBBandedColumn27: TcxGridDBBandedColumn;
    cxGridDBBandedColumn28: TcxGridDBBandedColumn;
    cxGridDBBandedColumn29: TcxGridDBBandedColumn;
    cxGridDBBandedColumn30: TcxGridDBBandedColumn;
    cxGridDBBandedColumn31: TcxGridDBBandedColumn;
    cxGridDBBandedColumn32: TcxGridDBBandedColumn;
    cxGridDBBandedColumn33: TcxGridDBBandedColumn;
    cxGridDBBandedColumn34: TcxGridDBBandedColumn;
    cxGridDBBandedColumn35: TcxGridDBBandedColumn;
    cxGridDBBandedColumn36: TcxGridDBBandedColumn;
    cxGridDBBandedColumn37: TcxGridDBBandedColumn;
    cxGridDBBandedColumn38: TcxGridDBBandedColumn;
    cxGridDBBandedColumn39: TcxGridDBBandedColumn;
    cxGridDBBandedColumn40: TcxGridDBBandedColumn;
    cxGridDBBandedColumn41: TcxGridDBBandedColumn;
    cxGridLevel6: TcxGridLevel;
    dsCari: TDataSource;
    qryCari: TZQuery;
    qryAntara: TZQuery;
    cxGridDBBandedTableView1Column1: TcxGridDBBandedColumn;
    cxGridDBBandedTableView1Column2: TcxGridDBBandedColumn;
    cxGridDBBandedTableView2Column1: TcxGridDBBandedColumn;
    cxGridDBBandedTableView2Column2: TcxGridDBBandedColumn;
    JvThread4: TJvThread;
    JvThreadSimpleDialog2: TJvThreadSimpleDialog;
    JvThreadSimpleDialog3: TJvThreadSimpleDialog;
    JvThread5: TJvThread;
    JvThread6: TJvThread;
    JvThreadSimpleDialog1: TJvThreadSimpleDialog;
    TabSheet11: TTabSheet;
    cxGrid9: TcxGrid;
    cxGridDBBandedTableView3: TcxGridDBBandedTableView;
    cxGridLevel7: TcxGridLevel;
    Panel15: TPanel;
    Label10: TLabel;
    Button9: TButton;
    cbKelUmur: TComboBox;
    qryCreate: TZQuery;
    Panel16: TPanel;
    Button12: TButton;
    ThreadKelUmur: TJvThread;
    ThreadDialogKelUmur: TJvThreadSimpleDialog;
    ThreadTPS: TJvThread;
    ThreadDialogTPS: TJvThreadSimpleDialog;
    qryWil: TZQuery;
    qryTanggal: TZQuery;
    qryKelUmur: TZQuery;
    cxGridDBBandedTableView3Column1: TcxGridDBBandedColumn;
    cxGridDBBandedTableView3Column2: TcxGridDBBandedColumn;
    cxGridDBBandedTableView3Column3: TcxGridDBBandedColumn;
    cxGridDBBandedTableView3Column4: TcxGridDBBandedColumn;
    cxGridDBBandedTableView3Column5: TcxGridDBBandedColumn;
    cxGridDBBandedTableView3Column6: TcxGridDBBandedColumn;
    cxGridDBBandedTableView3Column7: TcxGridDBBandedColumn;
    cxGridDBBandedTableView3Column8: TcxGridDBBandedColumn;
    cxGridDBBandedTableView3Column9: TcxGridDBBandedColumn;
    cxGridDBBandedTableView3Column10: TcxGridDBBandedColumn;
    cxGridDBBandedTableView3Column11: TcxGridDBBandedColumn;
    cxGridDBBandedTableView3Column12: TcxGridDBBandedColumn;
    cxGridDBBandedTableView3Column13: TcxGridDBBandedColumn;
    cxGridDBBandedTableView3Column14: TcxGridDBBandedColumn;
    cxGridDBBandedTableView3Column15: TcxGridDBBandedColumn;
    dsKelUmur: TDataSource;
    TabSheet12: TTabSheet;
    cxGrid10: TcxGrid;
    cxGridDBBandedTableView4: TcxGridDBBandedTableView;
    cxGridDBBandedColumn42: TcxGridDBBandedColumn;
    cxGridDBBandedColumn43: TcxGridDBBandedColumn;
    cxGridDBBandedColumn44: TcxGridDBBandedColumn;
    cxGridDBBandedColumn45: TcxGridDBBandedColumn;
    cxGridDBBandedColumn46: TcxGridDBBandedColumn;
    cxGridLevel8: TcxGridLevel;
    Panel17: TPanel;
    Label16: TLabel;
    Button15: TButton;
    cbKabKotTPS: TComboBox;
    Panel18: TPanel;
    Button16: TButton;
    qryJmlTPS: TZQuery;
    dsJmlTPS: TDataSource;
    TabSheet13: TTabSheet;
    Panel19: TPanel;
    Label17: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Button19: TButton;
    cbKelRT: TComboBox;
    cbKecRT: TComboBox;
    cbKabKotRT: TComboBox;
    Panel20: TPanel;
    Button20: TButton;
    cxGrid11: TcxGrid;
    cxGridDBBandedTableView5: TcxGridDBBandedTableView;
    cxGridDBBandedColumn47: TcxGridDBBandedColumn;
    cxGridDBBandedColumn48: TcxGridDBBandedColumn;
    cxGridDBBandedColumn49: TcxGridDBBandedColumn;
    cxGridDBBandedColumn50: TcxGridDBBandedColumn;
    cxGridDBBandedColumn51: TcxGridDBBandedColumn;
    cxGridLevel9: TcxGridLevel;
    cxGridDBBandedTableView5Column1: TcxGridDBBandedColumn;
    dsRT: TDataSource;
    qryRT: TZQuery;
    ThreadDialogRT: TJvThreadSimpleDialog;
    threadRT: TJvThread;
    cxGrid1DBBandedTableView1Column1: TcxGridDBBandedColumn;
    cxGrid1DBBandedTableView1Column2: TcxGridDBBandedColumn;
    TabSheet14: TTabSheet;
    Panel21: TPanel;
    Label21: TLabel;
    Button21: TButton;
    cbHapus: TComboBox;
    cxGrid12: TcxGrid;
    cxGridDBBandedTableView6: TcxGridDBBandedTableView;
    cxGridDBBandedColumn52: TcxGridDBBandedColumn;
    cxGridDBBandedColumn53: TcxGridDBBandedColumn;
    cxGridDBBandedColumn54: TcxGridDBBandedColumn;
    cxGridDBBandedColumn55: TcxGridDBBandedColumn;
    cxGridDBBandedColumn56: TcxGridDBBandedColumn;
    cxGridDBBandedColumn57: TcxGridDBBandedColumn;
    cxGridDBBandedColumn58: TcxGridDBBandedColumn;
    cxGridDBBandedColumn59: TcxGridDBBandedColumn;
    cxGridDBBandedColumn60: TcxGridDBBandedColumn;
    cxGridDBBandedColumn61: TcxGridDBBandedColumn;
    cxGridDBBandedColumn66: TcxGridDBBandedColumn;
    cxGridLevel10: TcxGridLevel;
    Panel22: TPanel;
    Button22: TButton;
    ThreadHapus: TJvThread;
    ThreadDialogHapus: TJvThreadSimpleDialog;
    qryHapus: TZQuery;
    dsHapus: TDataSource;
    TabSheet15: TTabSheet;
    Panel23: TPanel;
    Label22: TLabel;
    Button23: TButton;
    cbKabkotCacat: TComboBox;
    cxGrid13: TcxGrid;
    cxGridDBBandedTableView7: TcxGridDBBandedTableView;
    cxGridDBBandedColumn62: TcxGridDBBandedColumn;
    cxGridDBBandedColumn63: TcxGridDBBandedColumn;
    cxGridDBBandedColumn64: TcxGridDBBandedColumn;
    cxGridDBBandedColumn65: TcxGridDBBandedColumn;
    cxGridDBBandedColumn67: TcxGridDBBandedColumn;
    cxGridDBBandedColumn68: TcxGridDBBandedColumn;
    cxGridDBBandedColumn69: TcxGridDBBandedColumn;
    cxGridDBBandedColumn70: TcxGridDBBandedColumn;
    cxGridDBBandedColumn71: TcxGridDBBandedColumn;
    cxGridDBBandedColumn72: TcxGridDBBandedColumn;
    cxGridDBBandedColumn73: TcxGridDBBandedColumn;
    cxGridLevel11: TcxGridLevel;
    Panel24: TPanel;
    Button24: TButton;
    threadCacat: TJvThread;
    ThreadDialogCacat: TJvThreadSimpleDialog;
    qryCacat: TZQuery;
    dsCacat: TDataSource;
    TabSheet16: TTabSheet;
    Panel25: TPanel;
    Label23: TLabel;
    Button25: TButton;
    cbKabkotKK: TComboBox;
    cxGrid14: TcxGrid;
    cxGridDBBandedTableView8: TcxGridDBBandedTableView;
    cxGridDBBandedColumn74: TcxGridDBBandedColumn;
    cxGridDBBandedColumn75: TcxGridDBBandedColumn;
    cxGridDBBandedColumn76: TcxGridDBBandedColumn;
    cxGridDBBandedColumn77: TcxGridDBBandedColumn;
    cxGridDBBandedColumn78: TcxGridDBBandedColumn;
    cxGridDBBandedColumn79: TcxGridDBBandedColumn;
    cxGridDBBandedColumn80: TcxGridDBBandedColumn;
    cxGridDBBandedColumn81: TcxGridDBBandedColumn;
    cxGridLevel12: TcxGridLevel;
    Panel26: TPanel;
    Button26: TButton;
    threadKK: TJvThread;
    threadDialogKK: TJvThreadSimpleDialog;
    qryKK: TZQuery;
    dsKK: TDataSource;
    TabSheet17: TTabSheet;
    Panel27: TPanel;
    Label24: TLabel;
    Button27: TButton;
    cbKabkotRRT: TComboBox;
    cxGrid15: TcxGrid;
    cxGridDBBandedTableView9: TcxGridDBBandedTableView;
    cxGridDBBandedColumn82: TcxGridDBBandedColumn;
    cxGridDBBandedColumn83: TcxGridDBBandedColumn;
    cxGridDBBandedColumn84: TcxGridDBBandedColumn;
    cxGridDBBandedColumn85: TcxGridDBBandedColumn;
    cxGridDBBandedColumn86: TcxGridDBBandedColumn;
    cxGridDBBandedColumn87: TcxGridDBBandedColumn;
    cxGridDBBandedColumn88: TcxGridDBBandedColumn;
    cxGridDBBandedColumn89: TcxGridDBBandedColumn;
    cxGridLevel13: TcxGridLevel;
    Panel28: TPanel;
    Button28: TButton;
    cxGridDBBandedTableView9Column1: TcxGridDBBandedColumn;
    cxGridDBBandedTableView9Column2: TcxGridDBBandedColumn;
    threadRRT: TJvThread;
    threadDialogRRT: TJvThreadSimpleDialog;
    qryRRT: TZQuery;
    dsRRT: TDataSource;
    TabSheet18: TTabSheet;
    TabSheet19: TTabSheet;
    Panel29: TPanel;
    Label25: TLabel;
    Button29: TButton;
    cbKabkotTb: TComboBox;
    cxGrid16: TcxGrid;
    cxGridDBBandedTableView10: TcxGridDBBandedTableView;
    cxGridDBBandedColumn90: TcxGridDBBandedColumn;
    cxGridDBBandedColumn91: TcxGridDBBandedColumn;
    cxGridDBBandedColumn92: TcxGridDBBandedColumn;
    cxGridDBBandedColumn93: TcxGridDBBandedColumn;
    cxGridDBBandedColumn94: TcxGridDBBandedColumn;
    cxGridDBBandedColumn95: TcxGridDBBandedColumn;
    cxGridDBBandedColumn96: TcxGridDBBandedColumn;
    cxGridDBBandedColumn97: TcxGridDBBandedColumn;
    cxGridDBBandedColumn98: TcxGridDBBandedColumn;
    cxGridDBBandedColumn99: TcxGridDBBandedColumn;
    cxGridLevel14: TcxGridLevel;
    Panel30: TPanel;
    Button30: TButton;
    Panel31: TPanel;
    Label26: TLabel;
    Button31: TButton;
    cbKabkotTbTPS: TComboBox;
    cxGrid17: TcxGrid;
    cxGridDBBandedTableView11: TcxGridDBBandedTableView;
    cxGridDBBandedColumn100: TcxGridDBBandedColumn;
    cxGridDBBandedColumn101: TcxGridDBBandedColumn;
    cxGridDBBandedColumn102: TcxGridDBBandedColumn;
    cxGridDBBandedColumn103: TcxGridDBBandedColumn;
    cxGridDBBandedColumn104: TcxGridDBBandedColumn;
    cxGridDBBandedColumn105: TcxGridDBBandedColumn;
    cxGridDBBandedColumn106: TcxGridDBBandedColumn;
    cxGridDBBandedColumn107: TcxGridDBBandedColumn;
    cxGridLevel15: TcxGridLevel;
    Panel32: TPanel;
    Button32: TButton;
    ThreadTb: TJvThread;
    threadDialogTb: TJvThreadSimpleDialog;
    qryTb: TZQuery;
    dsTb: TDataSource;
    ThreadTbTPS: TJvThread;
    ThreadDialogTbTPS: TJvThreadSimpleDialog;
    qryTbTPS: TZQuery;
    dsTbTPS: TDataSource;
    procedure FormActivate(Sender: TObject);
    procedure cbKabkot3Change(Sender: TObject);
    procedure cbKec3Change(Sender: TObject);
    procedure cbKel3Change(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure cbKabkot4Change(Sender: TObject);
    procedure cbKec4Change(Sender: TObject);
    procedure cbKel4Change(Sender: TObject);
    procedure cbKabkot5Change(Sender: TObject);
    procedure cbKec5Change(Sender: TObject);
    procedure cbKel5Change(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure cbKabkot2Change(Sender: TObject);
    procedure cbKabKot1Change(Sender: TObject);
    procedure Button14Click(Sender: TObject);
    procedure JvThread1Execute(Sender: TObject; Params: Pointer);
    procedure JvThread1Begin(Sender: TObject);
    procedure JvThread1Finish(Sender: TObject);
    procedure JvThread2Execute(Sender: TObject; Params: Pointer);
    procedure JvThread2Begin(Sender: TObject);
    procedure JvThread2Finish(Sender: TObject);
    procedure Button17Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button13Click(Sender: TObject);
    procedure JvThread3Execute(Sender: TObject; Params: Pointer);
    procedure JvThread3Begin(Sender: TObject);
    procedure JvThread3Finish(Sender: TObject);
    procedure Button18Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure PageControl2Change(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure cbkel6Change(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure cbkec6Change(Sender: TObject);
    procedure JvThread5Execute(Sender: TObject; Params: Pointer);
    procedure JvThread5FinishAll(Sender: TObject);
    procedure JvThread4Execute(Sender: TObject; Params: Pointer);
    procedure JvThread4FinishAll(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure JvThread2FinishAll(Sender: TObject);
    procedure JvThread3FinishAll(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure ThreadKelUmurExecute(Sender: TObject; Params: Pointer);
    procedure ThreadKelUmurFinishAll(Sender: TObject);
    procedure Button12Click(Sender: TObject);
    procedure Button16Click(Sender: TObject);
    procedure Button15Click(Sender: TObject);
    procedure ThreadTPSExecute(Sender: TObject; Params: Pointer);
    procedure ThreadTPSFinishAll(Sender: TObject);
    procedure Button20Click(Sender: TObject);
    procedure Button19Click(Sender: TObject);
    procedure threadRTExecute(Sender: TObject; Params: Pointer);
    procedure threadRTFinishAll(Sender: TObject);
    procedure cbKabKotRTChange(Sender: TObject);
    procedure cbKecRTChange(Sender: TObject);
    procedure cbKelRTChange(Sender: TObject);
    procedure ThreadHapusExecute(Sender: TObject; Params: Pointer);
    procedure ThreadHapusFinishAll(Sender: TObject);
    procedure Button21Click(Sender: TObject);
    procedure Button22Click(Sender: TObject);
    procedure Button24Click(Sender: TObject);
    procedure Button23Click(Sender: TObject);
    procedure threadCacatExecute(Sender: TObject; Params: Pointer);
    procedure threadCacatFinishAll(Sender: TObject);
    procedure Button26Click(Sender: TObject);
    procedure Button25Click(Sender: TObject);
    procedure threadKKFinishAll(Sender: TObject);
    procedure threadKKExecute(Sender: TObject; Params: Pointer);
    procedure Button28Click(Sender: TObject);
    procedure threadRRTFinishAll(Sender: TObject);
    procedure threadRRTExecute(Sender: TObject; Params: Pointer);
    procedure Button27Click(Sender: TObject);
    procedure ThreadTbExecute(Sender: TObject; Params: Pointer);
    procedure ThreadTbFinishAll(Sender: TObject);
    procedure Button29Click(Sender: TObject);
    procedure Button32Click(Sender: TObject);
    procedure Button30Click(Sender: TObject);
    procedure Button31Click(Sender: TObject);
    procedure ThreadTbTPSExecute(Sender: TObject; Params: Pointer);
    procedure ThreadTbTPSFinishAll(Sender: TObject);
    
  private
    { Private declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent; kode_login: String);             overload;
  end;

var
  frmInformasi: TfrmInformasi;
  kdKab,kdKec,kdKel:string;
  slogin, skota, skec:string;
implementation

uses StrUtils, Setting;

{$R *.dfm}
constructor TfrmInformasi.Create(AOwner: TComponent; kode_login: String);
begin
    slogin:=kode_login;
    inherited Create(AOwner);
end;

procedure TfrmInformasi.FormActivate(Sender: TObject);
var
  setting:TSetting_Form;
begin
  //showmessage('a');
  setting:=TSetting_Form.Create;
  con.HostName:=setting.host;
  con.Database:=setting.database;
  con.User:=setting.user;
  con.Password:=setting.password;

  con.Connected:=True;

  cbKabKot1.Items.Clear;
  cbKabKot2.Items.Clear;
  cbKabKot3.Items.Clear;
  cbKabKot4.Items.Clear;
  cbKabKot5.Items.Clear;
  cbKabKot6.Items.Clear;
  cbKelUmur.Items.Clear;
  cbKabkotTps.Items.Clear;
  cbKabkotRT.Items.Clear;
  cbHapus.Items.Clear;
  cbKabkotTb.Items.Clear;
  cbKabkotTbTps.Items.Clear;
  //showmessage('a1');
  if(slogin <> '00') then
  begin
    skota := copy(slogin, 1, 2);
    kdKab := skota;
    qryKabKot.Active:=False;
    qryKabKot.SQL.Clear;
    qryKabKot.SQL.Add('SELECT * FROM kabkot WHERE KODE = '+quotedstr(skota));
    qryKabKot.Active:=True;
    //showmessage('a1');
    while not qryKabKot.Eof do
    begin
      cbKabKot1.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
      cbKabKot1.ItemIndex := 0;
      cbKabkot1.Enabled := false;
      cbKabKot2.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
      cbKabKot2.ItemIndex := 0;
      cbKabkot2.Enabled := false;
      cbKabKot3.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
      cbKabKot3.ItemIndex := 0;
      cbKabkot3.Enabled := false;
      cbKabKot4.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
      cbKabKot4.ItemIndex := 0;
      cbKabkot4.Enabled := false;
      cbKabKot5.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
      cbKabKot5.ItemIndex := 0;
      cbKabkot5.Enabled := false;
      cbKabKot6.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
      cbKabKot6.ItemIndex := 0;
      cbKabkot6.Enabled := false;
      cbKelUmur.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
      cbKelUmur.ItemIndex := 0;
      cbKelUmur.Enabled := false;
      cbKabKotTps.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
      cbKabKotTps.ItemIndex := 0;
      cbKabkotTps.Enabled := false;
      cbKabKotRT.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
      cbKabKotRT.ItemIndex := 0;
      cbKabkotRT.Enabled := false;
      cbHapus.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
      cbHapus.ItemIndex := 0;
      cbHapus.Enabled := false;
      cbKabkotCacat.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
      cbKabkotCacat.ItemIndex := 0;
      cbKabkotCacat.Enabled := false;
      cbKabkotKK.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
      cbKabkotKK.ItemIndex := 0;
      cbKabkotKK.Enabled := false;
      cbKabkotRRT.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
      cbKabkotRRT.ItemIndex := 0;
      cbKabkotRRT.Enabled := false;
      cbKabkotTb.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
      cbKabkotTb.ItemIndex := 0;
      cbKabkotTb.Enabled := false;
      cbKabkotTbTPS.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
      cbKabkotTbTPS.ItemIndex := 0;
      cbKabkotTbTPS.Enabled := false;
      qryKabKot.Next;
    end;

    //load kecamatan
    if(length(slogin) >2) then
    begin
      skec := slogin;
      kdKec := slogin;
      qryKec.Active:=False;
      qryKec.SQL.Clear;
      qryKec.SQL.Add('SELECT * FROM kecamatan WHERE KODE_KECAMATAN = '+quotedstr(skec));
      qryKec.Active:=True;
      //showmessage('a2');
    end
    else
    begin
      qryKec.Active:=False;
      qryKec.SQL.Clear;
      qryKec.SQL.Add('SELECT * FROM kecamatan WHERE KODE_KABKOT = '+quotedstr(kdKab));
      qryKec.Active:=True;
      //showmessage('a3');
    end;

    while not qryKec.Eof do
    begin
      cbKec3.Items.Add(qryKec.FieldByName('TAMPIL').AsString);
      cbKec4.Items.Add(qryKec.FieldByName('TAMPIL').AsString);
      cbKec5.Items.Add(qryKec.FieldByName('TAMPIL').AsString);
      cbKec6.Items.Add(qryKec.FieldByName('TAMPIL').AsString);
      cbKecRT.Items.Add(qryKec.FieldByName('TAMPIL').AsString);
      qryKec.Next;
    end;

    //load kelurahan
    if(length(slogin) >2) then
    begin
      cbKec3.ItemIndex:=0;
      cbKec3.Enabled:=false;
      cbKec4.ItemIndex:=0;
      cbKec4.Enabled:=false;
      cbKec5.ItemIndex:=0;
      cbKec5.Enabled:=false;
      cbKec6.ItemIndex:=0;
      cbKec6.Enabled:=false;
      cbKecRT.ItemIndex:=0;
      cbKecRT.Enabled:=false;

      qryKel.Active:=False;
      qryKel.SQL.Clear;
      qryKel.SQL.Add('SELECT * FROM desa WHERE KODE_KECAMATAN = '+quotedstr(kdKec));
      qryKel.Active:=True;
      //showmessage('a4');
      while not qryKel.Eof do
      begin
        cbKel3.Items.Add(qryKel.FieldByName('TAMPIL').AsString);
        cbKel4.Items.Add(qryKel.FieldByName('TAMPIL').AsString);
        cbKel5.Items.Add(qryKel.FieldByName('TAMPIL').AsString);
        cbKelRT.Items.Add(qryKel.FieldByName('TAMPIL').AsString);
//      cbKabKot6.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
        qryKel.Next;
      end;
    end;
  end

  else begin //kalo slogin = 00;
  qryKabKot.Active:=False;
  qryKabKot.SQL.Clear;
  qryKabKot.SQL.Add('SELECT * FROM kabkot');
  qryKabKot.Active:=True;

  while not qryKabKot.Eof do
  begin
    cbKabKot1.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
    cbKabKot2.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
    cbKabKot3.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
    cbKabKot4.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
    cbKabKot5.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
    cbKabKot6.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
    cbKelUmur.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
    cbKabkotTps.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
    cbKabKotRT.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
    cbHapus.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
    cbKabkotCacat.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
    cbKabkotKK.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
    cbKabkotRRT.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
    cbKabkotTbTps.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
    cbKabkotTb.Items.Add(qryKabKot.FieldByName('TAMPIL').AsString);
    qryKabKot.Next;
  end;
  end;
  //showmessage('b');
end;

procedure TfrmInformasi.cbKabkot3Change(Sender: TObject);
begin
  qryKabKot.Active:=False;
  qryKabKot.SQL.Clear;
  qryKabKot.SQL.Add('SELECT * FROM kabkot WHERE TAMPIL='+QuotedStr(cbKabkot3.Items[cbKabkot3.ItemIndex]));
  qryKabKot.Active:=True;
  kdKab:=qryKabKot.FieldByName('KODE').AsString;

  qryKec.Active:=False;
  qryKec.SQL.Clear;
  qryKec.SQL.Add('SELECT * FROM kecamatan WHERE KODE_KABKOT='+QuotedStr(kdKab));
  qryKec.Active:=True;

  cbKec3.Items.Clear;
  while not qryKec.Eof do
  begin
    cbKec3.Items.Add(qryKec.FieldByName('TAMPIL').AsString);
    qryKec.Next;
  end;
end;

procedure TfrmInformasi.cbKec3Change(Sender: TObject);
begin
  qryKec.Active:=False;
  qryKec.SQL.Clear;
  qryKec.SQL.Add('SELECT * FROM kecamatan WHERE TAMPIL='+QuotedStr(cbKec3.Items[cbKec3.ItemIndex])+' AND KODE_KABKOT='+QuotedStr(kdKab));
  qryKec.Active:=True;
  kdKec:=qryKec.FieldByName('KODE_KECAMATAN').AsString;

  qryKel.Active:=False;
  qryKel.SQL.Clear;
  qryKel.SQL.Add('SELECT * FROM desa WHERE KODE_KABKOT='+QuotedStr(kdKab)+' AND KODE_KECAMATAN='+QuotedStr(kdKec));
  qryKel.Active:=True;

  cbKel3.Items.Clear;
  while not qryKel.Eof do
  begin
    cbKel3.Items.Add(qryKel.FieldByName('TAMPIL').AsString);
    qryKel.Next;
  end;
end;

procedure TfrmInformasi.cbKel3Change(Sender: TObject);
begin
  qryKel.Active:=False;
  qryKel.SQL.Clear;
  qryKel.SQL.Add('SELECT * FROM desa WHERE TAMPIL='+QuotedStr(cbKel3.Items[cbKel3.ItemIndex])+' AND KODE_KABKOT='+QuotedStr(kdKab)+' AND KODE_KECAMATAN='+QuotedStr(kdKec));
  qryKel.Active:=True;
  kdKel:=qryKel.FieldByName('KODE_DESA').AsString;
end;

procedure TfrmInformasi.Button3Click(Sender: TObject);
begin
  if cbKel3.Text<>'' then
  begin
    gridTableBand.Bands[0].Caption:='DATA NIK TIDAK SESUAI '+cbKabkot3.Text+', KECAMATAN '+cbKec3.Text+', KELURAHAN/DESA '+cbKel3.Text;
    qryCek.Active:=False;
    qryCek.SQL.Clear;
    qryCek.SQL.Add('SELECT * FROM information_schema.`TABLES` WHERE table_schema='+QuotedStr(con.Database)+' AND TABLE_NAME='+QuotedStr('pemilih_'+kdKel));
    qryCek.Active:=True;

    if qryCek.RecordCount=1 then
    begin
      qryNIKKurang.Active:=False;
      qryNIKKurang.SQL.Clear;
      qryNIKKurang.SQL.Add('SELECT a.*,LENGTH(a.NIK) AS panjang_nik, b.NAMA AS NAMA_TPS FROM pemilih_'+kdKel+' a LEFT JOIN daftar_tps b ON a.TPS = b.ID_TPS WHERE LENGTH(a.NIK)<> 16 AND a.STATUS2=0');
      qryNIKKurang.Active:=True;
    end
    else
      begin
        qryNIKKurang.Active:=False;
      end;
  end
  else
    begin
      gridTableBand.Bands[0].Caption:='';
      MessageDlg('Kecamatan dan Kelurahan harus dipilih',mtError,[mbOk],0);
    end;
end;

procedure TfrmInformasi.Button8Click(Sender: TObject);
var
  qry,kodes:string;
  tgl:string;
begin
  if cbKel4.Text<>'' then
  begin
  cxGrid4DBBandedTableView1.Bands[0].Caption:='DATA DIBAWAH 17 / DIATAS 100 TAHUN '+cbKabkot4.Text+', KECAMATAN '+cbKec4.Text+', KELURAHAN/DESA '+cbKel4.Text;
  qryCek.Active:=False;
  qryCek.SQL.Clear;
  qryCek.SQL.Add('SELECT * FROM information_schema.`TABLES` WHERE table_schema='+QuotedStr(con.Database)+' AND TABLE_NAME='+QuotedStr('pemilih_'+kdKel));
  qryCek.Active:=True;

    if qryCek.RecordCount=1 then
    begin
      qryBantu.Active:=False;
      qryBantu.SQL.Clear;
      qryBantu.SQL.Add('SELECT * FROM info_tanggal WHERE aktif=1');
      qryBantu.Active:=True;
      tgl:=FormatDateTime('yyyy-MM-dd',qryBantu.FieldByName('tanggal').AsDateTime);

      qry:='SELECT a.*,DATE_FORMAT(FROM_DAYS(TO_DAYS('+QuotedStr(tgl)+')-TO_DAYS(CONCAT(RIGHT(a.TANGGAL,4),''-'',MID(a.TANGGAL,4,2),''-'',LEFT(a.TANGGAL,2)))), ''%Y'')+0 AS age, b.NAMA AS NAMA_TPS FROM pemilih_'+kdKel+' a LEFT JOIN daftar_tps b ON a.TPS = b.ID_TPS'
        +' WHERE (DATE_FORMAT(FROM_DAYS(TO_DAYS('+QuotedStr(tgl)+')-TO_DAYS(CONCAT(RIGHT(a.TANGGAL,4),''-'',MID(a.TANGGAL,4,2),''-'',LEFT(a.TANGGAL,2)))), ''%Y'')+0 < 17 '
        +' OR DATE_FORMAT(FROM_DAYS(TO_DAYS('+QuotedStr(tgl)+')-TO_DAYS(CONCAT(RIGHT(a.TANGGAL,4),''-'',MID(a.TANGGAL,4,2),''-'',LEFT(a.TANGGAL,2)))), ''%Y'')+0 >= 100) AND STATUS2=0';
      qryUmurKurang.Active:=False;
      qryUmurKurang.SQL.Clear;
      qryUmurKurang.SQL.Add(qry);
      qryUmurKurang.Active:=True;
    end
    else
    begin
      qryUmurKurang.Active:=False;
    end;
  end
  else
  begin
    cxGrid5DBBandedTableView1.Bands[0].Caption:='';
      MessageDlg('Kecamatan dan Kelurahan harus dipilih',mtError,[mbOk],0);
    end;
  {else if cbKec4.Text<>'' then
  begin
    showmessage(kdKec);
    cxGrid4DBBandedTableView1.Bands[0].Caption:='DATA DIBAWAH 17 / DIATAS 100 TAHUN '+cbKabkot4.Text+', KECAMATAN '+cbKec4.Text;
    qryCek.Active:=False;
    qryCek.SQL.Clear;
    qryCek.SQL.Add('SELECT TABLE_NAME FROM information_schema.`TABLES` WHERE table_schema='+QuotedStr(con.Database)+' AND TABLE_NAME LIKE '+QuotedStr('pemilih_'+kdKec+'%'));
    qryCek.Active:=True;

    while not qryCek.eof do
    begin
      kodes:=qryCek.Fields[0].AsString;
      qryBantu.Active:=False;
      qryBantu.SQL.Clear;
      qryBantu.SQL.Add('SELECT * FROM info_tanggal WHERE aktif=1');
      qryBantu.Active:=True;
      tgl:=FormatDateTime('yyyy-MM-dd',qryBantu.FieldByName('tanggal').AsDateTime);

      qry:='SELECT a.*,DATE_FORMAT(FROM_DAYS(TO_DAYS('+QuotedStr(tgl)+')-TO_DAYS(CONCAT(RIGHT(a.TANGGAL,4),''-'',MID(a.TANGGAL,4,2),''-'',LEFT(a.TANGGAL,2)))), ''%Y'')+0 AS age, b.NAMA AS NAMA_TPS FROM '+kodes+' a LEFT JOIN daftar_tps b ON a.TPS = b.ID_TPS'
        +' WHERE (DATE_FORMAT(FROM_DAYS(TO_DAYS('+QuotedStr(tgl)+')-TO_DAYS(CONCAT(RIGHT(a.TANGGAL,4),''-'',MID(a.TANGGAL,4,2),''-'',LEFT(a.TANGGAL,2)))), ''%Y'')+0 < 17 '
        +' OR DATE_FORMAT(FROM_DAYS(TO_DAYS('+QuotedStr(tgl)+')-TO_DAYS(CONCAT(RIGHT(a.TANGGAL,4),''-'',MID(a.TANGGAL,4,2),''-'',LEFT(a.TANGGAL,2)))), ''%Y'')+0 >= 100) AND STATUS2=0';
      qryUmurKurang.Active:=False;
      qryUmurKurang.SQL.Clear;
      qryUmurKurang.SQL.Add(qry);
      qryUmurKurang.Active:=True;
      qrycek.Next;
    end;
  end
  else
  begin

  end;}
end;

procedure TfrmInformasi.cbKabkot4Change(Sender: TObject);
begin
  qryKabKot.Active:=False;
  qryKabKot.SQL.Clear;
  qryKabKot.SQL.Add('SELECT * FROM kabkot WHERE TAMPIL='+QuotedStr(cbKabkot4.Items[cbKabkot4.ItemIndex]));
  qryKabKot.Active:=True;
  kdKab:=qryKabKot.FieldByName('KODE').AsString;

  qryKec.Active:=False;
  qryKec.SQL.Clear;
  qryKec.SQL.Add('SELECT * FROM kecamatan WHERE KODE_KABKOT='+QuotedStr(kdKab));
  qryKec.Active:=True;

  cbKec4.Items.Clear;
  while not qryKec.Eof do
  begin
    cbKec4.Items.Add(qryKec.FieldByName('TAMPIL').AsString);
    qryKec.Next;
  end;
end;

procedure TfrmInformasi.cbKec4Change(Sender: TObject);
begin
  qryKec.Active:=False;
  qryKec.SQL.Clear;
  qryKec.SQL.Add('SELECT * FROM kecamatan WHERE TAMPIL='+QuotedStr(cbKec4.Items[cbKec4.ItemIndex])+' AND KODE_KABKOT='+QuotedStr(kdKab));
  qryKec.Active:=True;
  kdKec:=qryKec.FieldByName('KODE_KECAMATAN').AsString;

  qryKel.Active:=False;
  qryKel.SQL.Clear;
  qryKel.SQL.Add('SELECT * FROM desa WHERE KODE_KABKOT='+QuotedStr(kdKab)+' AND KODE_KECAMATAN='+QuotedStr(kdKec));
  qryKel.Active:=True;

  cbKel4.Items.Clear;
  while not qryKel.Eof do
  begin
    cbKel4.Items.Add(qryKel.FieldByName('TAMPIL').AsString);
    qryKel.Next;
  end;
end;

procedure TfrmInformasi.cbKel4Change(Sender: TObject);
begin
  qryKel.Active:=False;
  qryKel.SQL.Clear;
  qryKel.SQL.Add('SELECT * FROM desa WHERE TAMPIL='+QuotedStr(cbKel4.Items[cbKel4.ItemIndex])+' AND KODE_KABKOT='+QuotedStr(kdKab)+' AND KODE_KECAMATAN='+QuotedStr(kdKec));
  qryKel.Active:=True;
  kdKel:=qryKel.FieldByName('KODE_DESA').AsString;
end;

procedure TfrmInformasi.cbKabkot5Change(Sender: TObject);
begin
  qryKabKot.Active:=False;
  qryKabKot.SQL.Clear;
  qryKabKot.SQL.Add('SELECT * FROM kabkot WHERE TAMPIL='+QuotedStr(cbKabkot5.Items[cbKabkot5.ItemIndex]));
  qryKabKot.Active:=True;
  kdKab:=qryKabKot.FieldByName('KODE').AsString;

  qryKec.Active:=False;
  qryKec.SQL.Clear;
  qryKec.SQL.Add('SELECT * FROM kecamatan WHERE KODE_KABKOT='+QuotedStr(kdKab));
  qryKec.Active:=True;

  cbKec5.Items.Clear;
  while not qryKec.Eof do
  begin
    cbKec5.Items.Add(qryKec.FieldByName('TAMPIL').AsString);
    qryKec.Next;
  end;
end;

procedure TfrmInformasi.cbKec5Change(Sender: TObject);
begin
  qryKec.Active:=False;
  qryKec.SQL.Clear;
  qryKec.SQL.Add('SELECT * FROM kecamatan WHERE TAMPIL='+QuotedStr(cbKec5.Items[cbKec5.ItemIndex])+' AND KODE_KABKOT='+QuotedStr(kdKab));
  qryKec.Active:=True;
  kdKec:=qryKec.FieldByName('KODE_KECAMATAN').AsString;

  qryKel.Active:=False;
  qryKel.SQL.Clear;
  qryKel.SQL.Add('SELECT * FROM desa WHERE KODE_KABKOT='+QuotedStr(kdKab)+' AND KODE_KECAMATAN='+QuotedStr(kdKec));
  qryKel.Active:=True;

  cbKel5.Items.Clear;
  while not qryKel.Eof do
  begin
    cbKel5.Items.Add(qryKel.FieldByName('TAMPIL').AsString);
    qryKel.Next;
  end;
end;

procedure TfrmInformasi.cbKel5Change(Sender: TObject);
begin
  qryKel.Active:=False;
  qryKel.SQL.Clear;
  qryKel.SQL.Add('SELECT * FROM desa WHERE TAMPIL='+QuotedStr(cbKel5.Items[cbKel5.ItemIndex])+' AND KODE_KABKOT='+QuotedStr(kdKab)+' AND KODE_KECAMATAN='+QuotedStr(kdKec));
  qryKel.Active:=True;
  kdKel:=qryKel.FieldByName('KODE_DESA').AsString;
end;

procedure TfrmInformasi.Button11Click(Sender: TObject);
var
  qry:string;
begin
  if cbKel5.Text<>'' then
  begin
  cxGrid5DBBandedTableView1.Bands[0].Caption:='REKAP DATA PEMILIH DIHAPUS '+cbKabkot5.Text+', KECAMATAN '+cbKec5.Text+', KELURAHAN/DESA '+cbKel5.Text;
  qryCek.Active:=False;
  qryCek.SQL.Clear;
  qryCek.SQL.Add('SELECT * FROM information_schema.`TABLES` WHERE table_schema='+QuotedStr(con.Database)+' AND TABLE_NAME='+QuotedStr('pemilih_'+kdKel));
  qryCek.Active:=True;

  if qryCek.RecordCount=1 then
  begin

    qry:='SELECT *,IF(STATUS2=1,"Meninggal Dunia",IF(STATUS2=2,"Data Fiktif",IF(STATUS2=3,"TNI / POLRI",IF(STATUS2=4,'
        +'"Pindah Alamat",IF(STATUS2=5,"HAPUS DATA",""))))) AS status FROM pemilih_'+kdKel+' WHERE STATUS2<>0';
    qryDPTHapus.Active:=False;
    qryDPTHapus.SQL.Clear;
    qryDPTHapus.SQL.Add(qry);
    qryDPTHapus.Active:=True;
  end
  else
    begin
      qryDPTHapus.Active:=False;
    end;
  end
  else
    begin
    cxGrid5DBBandedTableView1.Bands[0].Caption:='';
      MessageDlg('Kecamatan dan Kelurahan harus dipilih',mtError,[mbOk],0);
    end;
end;

procedure TfrmInformasi.cbKabkot2Change(Sender: TObject);
begin
  qryKabKot.Active:=False;
  qryKabKot.SQL.Clear;
  qryKabKot.SQL.Add('SELECT * FROM kabkot WHERE TAMPIL='+QuotedStr(cbKabkot2.Items[cbKabkot2.ItemIndex]));
  qryKabKot.Active:=True;
  kdKab:=qryKabKot.FieldByName('KODE').AsString;
end;

procedure TfrmInformasi.cbKabKot1Change(Sender: TObject);
begin
  qryKabKot.Active:=False;
  qryKabKot.SQL.Clear;
  qryKabKot.SQL.Add('SELECT * FROM kabkot WHERE TAMPIL='+QuotedStr(cbKabkot1.Items[cbKabkot1.ItemIndex]));
  qryKabKot.Active:=True;
  kdKab:=qryKabKot.FieldByName('KODE').AsString;
end;

procedure TfrmInformasi.Button14Click(Sender: TObject);
begin
  JvThread1.Execute(Self);

end;

procedure TfrmInformasi.JvThread1Execute(Sender: TObject; Params: Pointer);
var
  proses_aktif :integer;
  tnd: integer;
  qry,qryz:WideString;
  kode:string;
  tabel, tambahan_where:string;
  mfile:textFile;
  total,jmlp,jmll:longint;
  tbtotal,tbjmlp,tbjmll:longint;
  ttotal,tjmlp,tjmll:longint;
begin
  //inisiasi
  total:=0;
  jmlp:=0;
  jmll:=0;
  tbtotal:=0;
  tbjmlp:=0;
  tbjmll:=0;
  ttotal:=0;
  tjmlp:=0;
  tjmll:=0;
  //bikin tabel jika belum ada
  qryz := 'CREATE TABLE IF NOT EXISTS `rekap_pemilih_chart` ('
  +'`id` int(11) NOT NULL AUTO_INCREMENT,'
  +'`tanggal` date DEFAULT NULL,'
  +'`informasi` varchar(100) DEFAULT NULL,'
  +'`dp4_jml` int(11) DEFAULT NULL,'
  +'`dp4_l` int(11) DEFAULT NULL,'
  +'`dp4_p` int(11) DEFAULT NULL,'
  +'`dps_jml` int(11) DEFAULT NULL,'
  +'`dps_l` int(11) DEFAULT NULL,'
  +'`dps_p` int(11) DEFAULT NULL,'
  +'`dpt_jml` int(11) DEFAULT NULL,'
  +'`dpt_l` int(11) DEFAULT NULL,'
  +'`dpt_p` int(11) DEFAULT NULL,'
  +'PRIMARY KEY (`id`)'
  +') ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2' ;

  qry2.Active:=False;
  qry2.SQL.Clear;
  qry2.SQL.Add(qryz);
  qry2.ExecSQL;

  //cek tabel udah berisi belum
  qry2.Active:=False;
  qry2.SQL.Clear;
  qry2.SQL.Add('SELECT id FROM rekap_pemilih_chart');
  qry2.Active:=true;

  //jika belum
  if qry2.RecordCount = 0 then begin
    //bikin tabel jika belum ada
    qryz := 'INSERT INTO `rekap_pemilih_chart` VALUES '
    +'(1, ''2012-11-13'', ''PILGUB 2013'', 0, 0, 0, 0, 0, 0, 0, 0, 0)';

    qryCreate.Active:=False;
    qryCreate.SQL.Clear;
    qryCreate.SQL.Add(qryz);
    qryCreate.ExecSQL;
  end;

  total := 0;
  jmlp := 0;
  jmll := 0;
  //cari proses aktif nya dulu
  qry := 'SELECT PROSES FROM proses_berjalan WHERE TANGGAL_MULAI <= NOW() AND TANGGAL_SELESAI >= NOW()';
  qry2.Active:=false;
  qry2.SQL.Clear;
  qry2.SQL.Add(qry);
  qry2.Active:=true;
  qry2.First;
  if(qry2.RecordCount>0) then
  begin
  proses_aktif := qry2.Fields[0].AsInteger;
    with(qry2) do begin
      while (not Eof) do
      begin

        Next;
      end;
    end;
  end;

  qryCek.Active:=False;
  qryCek.SQL.Clear;
  qryCek.SQL.Add('SHOW TABLES WHERE Tables_in_'+con.Database+' LIKE ''pemilih_%''');
  qryCek.Active:=True;
  qryCek.First;

  with qryCek do
  begin
    while not eof do
    begin
      tabel:= FieldByName('Tables_in_'+con.Database).asString;
      if tabel <> 'pemilih_ganda' then begin
        tambahan_where := 'STATUS2 = ''0'' AND TPS <> 0 AND STATUS1 <>''1'' AND STATUS1 <> ''3''';
        qry:='SELECT (SELECT COUNT(ID_USER) AS HITUNG FROM '+tabel+' WHERE '+tambahan_where+' AND (KELAMIN = '+QuotedStr('L')+' OR KELAMIN = '+QuotedStr('P')+'))';
        qry:=qry+', (SELECT COUNT(ID_USER) AS HITUNG FROM '+tabel+' WHERE KELAMIN = '+QuotedStr('L')+' AND '+tambahan_where+')';
        qry:=qry+', (SELECT COUNT(ID_USER) AS HITUNG FROM '+tabel+' WHERE KELAMIN = '+QuotedStr('P')+' AND '+tambahan_where+')';
        tambahan_where := 'STATUS2 = ''0'' AND TPS <> 0 AND STATUS1 =''1''';
        qry:=qry+', (SELECT COUNT(ID_USER) AS HITUNG FROM '+tabel+' WHERE '+tambahan_where+')';
        qry:=qry+', (SELECT COUNT(ID_USER) AS HITUNG FROM '+tabel+' WHERE KELAMIN = '+QuotedStr('L')+' AND '+tambahan_where+')';
        qry:=qry+', (SELECT COUNT(ID_USER) AS HITUNG FROM '+tabel+' WHERE KELAMIN = '+QuotedStr('P')+' AND '+tambahan_where+')';
        tambahan_where := 'STATUS2 = ''0'' AND TPS <> 0';
        qry:=qry+', (SELECT COUNT(ID_USER) AS HITUNG FROM '+tabel+' WHERE '+tambahan_where+')';
        qry:=qry+', (SELECT COUNT(ID_USER) AS HITUNG FROM '+tabel+' WHERE KELAMIN = '+QuotedStr('L')+' AND '+tambahan_where+')';
        qry:=qry+', (SELECT COUNT(ID_USER) AS HITUNG FROM '+tabel+' WHERE KELAMIN = '+QuotedStr('P')+' AND '+tambahan_where+')';
        //showmessage(qry);
        //memo1.Text:=memo1.Text+qry+' | ';

        qry2.Active:=false;
        qry2.SQL.Clear;
        qry2.SQL.Add(qry);
        qry2.Active:=true;
        qry2.First;
        tnd :=0;
        with(qry2) do
        begin
          while (not Eof) do
          begin


              total := total + Fields[0].AsInteger;

              jmll := jmll + Fields[1].AsInteger;

              jmlp := jmlp + Fields[2].AsInteger;

              tbtotal := tbtotal + Fields[3].AsInteger;

              tbjmll := tbjmll + Fields[4].AsInteger;

              tbjmlp := tbjmlp + Fields[5].AsInteger;

              ttotal := ttotal + Fields[6].AsInteger;

              tjmll := tjmll + Fields[7].AsInteger;

              tjmlp := tjmlp + Fields[8].AsInteger;

            Next;
          end;
        end;
      end;
      Next;
    end;
  end;

  qryz := 'dp4_jml = '+inttostr(total)+', dp4_l = '+inttostr(jmll)+', dp4_p = '+inttostr(jmlp);
  qryz := qryz+', dps_jml = '+inttostr(tbtotal)+', dps_l = '+inttostr(tbjmll)+', dps_p = '+inttostr(tbjmlp);
  qryz := qryz+', dpt_jml = '+inttostr(ttotal)+', dpt_l = '+inttostr(tjmll)+', dpt_p = '+inttostr(tjmlp);

  qryz := 'UPDATE rekap_pemilih_chart SET tanggal = NOW(), '+qryz;

  //memo1.Text:=qryz;

  qry2.Active:=False;
  qry2.SQL.Clear;
  qry2.SQL.Add(qryz);
  qry2.ExecSQL;

  //showmessage('berhasil');

  qryRekap.Active:=False;
  qryRekap.Active:=True;

  //showmessage('ciyus');
end;

procedure TfrmInformasi.JvThread1Begin(Sender: TObject);
begin
WaitProgress1.Active:=True;
end;

procedure TfrmInformasi.JvThread1Finish(Sender: TObject);
begin
WaitProgress1.Active:=False;
end;

procedure TfrmInformasi.JvThread2Execute(Sender: TObject; Params: Pointer);
var
  qryX:TZQuery;
  qry,kel,qrycreate, sql_ins, kabkot, kec, des, kecsblm:string;
  jtps, jp, jl, jpr, jst, jbt, tps, p, l, pr, st, bt, jdesa: integer;
begin
  jdesa := 0;
  jtps := 0;
  jp := 0;
  jl := 0;
  jpr := 0;
  jst := 0;
  jbt := 0;
  
  cxGrid1DBBandedTableView1.Bands[0].Caption:='REKAP DATA PEMILIH '+cbKabKot1.Text;
  qryCek.Active:=False;
  qryCek.SQL.Clear;
  qryCek.SQL.Add('DROP TABLE IF EXISTS rekap_data_pemilih');
  qryCek.ExecSQL;

  kecsblm:='';
  kec:='';

  qrycreate := 'CREATE TABLE IF NOT EXISTS `rekap_data_pemilih` ('
  +'`id` int(11) NOT NULL AUTO_INCREMENT,'
  +'`kabkot` varchar(50) DEFAULT NULL,'
  +'`kecamatan` varchar(50) DEFAULT NULL,'
  +'`kelurahan` varchar(50) DEFAULT NULL,'
  +'`jml_tps` int(11) DEFAULT NULL,'
  +'`pemilih` int(11) DEFAULT NULL,'
  +'`sudah` int(11) DEFAULT NULL,'
  +'`belum` int(11) DEFAULT NULL,'
  +'`laki` int(11) DEFAULT NULL,'
  +'`perempuan` int(11) DEFAULT NULL,'
  +'PRIMARY KEY (`id`)'
  +') ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;';

  qryCek.Active:=False;
  qryCek.SQL.Clear;
  qryCek.SQL.Add(qrycreate);
  qryCek.ExecSQL;

  qryBantu.Active:=False;
  qryBantu.SQL.Clear;
  qryBantu.SQL.Add('SELECT * FROM desa WHERE LEFT(KODE_DESA,2)='+QuotedStr(kdKab)+' GROUP BY KODE_DESA');
//  qryBantu.SQL.Add('SELECT * FROM daftar_tps WHERE LEFT(KODE_DESA,2)='+QuotedStr(kdKab)+' GROUP BY KODE_DESA');
  qryBantu.Active:=True;
  with qryBantu do
  begin
    while not Eof do
    begin
      kel:=fieldByName('KODE_DESA').AsString;
//      ShowMessage(kel);
      qryX:=TZQuery.Create(Self);
      qryX.Connection:=con;
      qryX.Active:=False;
      qryX.SQL.Clear;
      qryX.SQL.Add(('SELECT * FROM information_schema.`TABLES` WHERE table_schema='+QuotedStr(con.Database)+' AND TABLE_NAME='+QuotedStr('pemilih_'+kel)));
      qryX.Active:=True;
//      ShowMessage(IntToStr(qryX.RecordCount));

      if qryX.RecordCount=1 then
      begin
        qry:='SELECT null,kabkot.TAMPIL AS kabkot,kecamatan.TAMPIL AS kecamatan,desa.TAMPIL AS kelurahan,(SELECT COUNT(*) FROM daftar_tps WHERE '+'KODE_DESA='+QuotedStr(kel)+') as tps,'
            +'(SELECT COUNT(ID_USER) FROM pemilih_'+kel+' WHERE STATUS2 = ''0'') AS total,(SELECT COUNT(ID_USER) FROM pemilih_'+kel+' WHERE TPS<>''0'' AND STATUS2 = ''0'') AS sudah,'
            +'(SELECT COUNT(ID_USER) FROM pemilih_'+kel+' WHERE TPS=''0'' AND STATUS2 = ''0'') AS belum, '
            +'(SELECT COUNT(ID_USER) FROM pemilih_'+kel+' WHERE KELAMIN=''L'' AND STATUS2 = ''0'') AS laki, '
            +'(SELECT COUNT(ID_USER) FROM pemilih_'+kel+' WHERE KELAMIN=''P'' AND STATUS2 = ''0'') AS perempuan '
            +'FROM kecamatan,desa,kabkot WHERE '
            +' LEFT(desa.KODE_DESA,5)=kecamatan.KODE_KECAMATAN AND desa.KODE_DESA='+QuotedStr(kel)
            +' AND kabkot.KODE=kecamatan.KODE_KABKOT GROUP BY desa.KODE_DESA';
        qryBantu2.Active:=False;
        qryBantu2.SQL.Clear;
        qryBantu2.SQL.Add(qry);
//        memo1.Lines.Add(qry);
        qryBantu2.Active:=true;
        qrybantu2.First;
        while not qryBantu2.Eof do begin
          kabkot := qryBantu2.fieldByName('kabkot').AsString;
          kec := qryBantu2.fieldByName('kecamatan').AsString;
          des := qryBantu2.fieldByName('kelurahan').AsString;
          tps := qryBantu2.fieldByName('tps').AsInteger;
          p := qryBantu2.fieldByName('total').AsInteger;
          l := qryBantu2.fieldByName('laki').AsInteger;
          pr := qryBantu2.fieldByName('perempuan').AsInteger;
          st := qryBantu2.fieldByName('sudah').AsInteger;
          bt := qryBantu2.fieldByName('belum').AsInteger;

          if(kec <> kecsblm) then begin
            if(kecsblm <> '') then begin
              //kecsblm := kec;
              sql_ins := 'INSERT INTO rekap_data_pemilih VALUES(NULL, '+quotedStr(kabkot)+', '+quotedStr('TOTAL '+kecsblm)+', '+quotedStr(inttostr(jdesa))+', '+quotedStr(inttostr(jtps))+', '+quotedStr(inttostr(jp))+', '+quotedStr(inttostr(jst))+', '+quotedStr(inttostr(jbt))+', '+quotedStr(inttostr(jl))+', '+quotedStr(inttostr(jpr))+')';

              qry1.Active:=false;
              qry1.SQL.Clear;
              qry1.SQL.Text:=sql_ins;
              qry1.ExecSQL;

              jdesa := 0;
              jtps := 0;
              jp := 0;
              jl := 0;
              jpr := 0;
              jst := 0;
              jbt := 0;
              kecsblm := kec;
            end else kecsblm := kec;
          end;

          sql_ins := 'INSERT INTO rekap_data_pemilih VALUES(NULL, '+quotedStr(kabkot)+', '+quotedStr(kec)+', '+quotedStr(des)+', '+quotedStr(inttostr(tps))+', '+quotedStr(inttostr(p))+', '+quotedStr(inttostr(st))+', '+quotedStr(inttostr(bt))+', '+quotedStr(inttostr(l))+', '+quotedStr(inttostr(pr))+')';

          qry1.Active:=false;
          qry1.SQL.Clear;
          qry1.SQL.Text:=sql_ins;
          qry1.ExecSQL;

          jdesa := jdesa+1;
          jtps := jtps+tps;
          jp := jp+p;
          jl := jl+l;
          jpr := jpr+pr;
          jst := jst+st;
          jbt := jbt+bt;

          qryBantu2.Next;
        end;
      end;
      qryX.Close;
      qryX.Destroy;
      Next;

    end;
    if(kec <> '') then begin
      kecsblm := kec;
      sql_ins := 'INSERT INTO rekap_data_pemilih VALUES(NULL, '+quotedStr(kabkot)+', '+quotedStr('TOTAL '+kecsblm)+', '+quotedStr(inttostr(jdesa))+', '+quotedStr(inttostr(jtps))+', '+quotedStr(inttostr(jp))+', '+quotedStr(inttostr(jst))+', '+quotedStr(inttostr(jbt))+', '+quotedStr(inttostr(jl))+', '+quotedStr(inttostr(jpr))+')';

      qry1.Active:=false;
      qry1.SQL.Clear;
      qry1.SQL.Text:=sql_ins;
      qry1.ExecSQL;

      jdesa := 0;
      jtps := 0;
      jp := 0;
      jl := 0;
      jpr := 0;
      jst := 0;
      jbt := 0;
      kecsblm := kec;
    end;
  end;
  qryRekapDataPemilih.Active:=False;
  qryRekapDataPemilih.Active:=True;
end;

procedure TfrmInformasi.JvThread2Begin(Sender: TObject);
begin
cxGrid1.BeginUpdate;

  cxGrid1.EndUpdate;
WaitProgress1.Active:=True;
end;

procedure TfrmInformasi.JvThread2Finish(Sender: TObject);
begin
WaitProgress1.Active:=False;
end;

procedure TfrmInformasi.Button17Click(Sender: TObject);
begin
  JvThread2.Execute(Self);
end;

procedure TfrmInformasi.Button5Click(Sender: TObject);
begin
  try
    if openDlg.Execute then
      ExportGridToExcel(openDlg.FileName,cxGrid2,False);
  finally
    MessageDlg('Export Berhasil',mtConfirmation,[mbOk],0);
  end;
end;

procedure TfrmInformasi.Button2Click(Sender: TObject);
begin
  try
    if openDlg.Execute then
      ExportGridToExcel(openDlg.FileName,cxGrid1,False);
  finally
    MessageDlg('Export Berhasil',mtConfirmation,[mbOk],0);
  end;
end;

procedure TfrmInformasi.Button7Click(Sender: TObject);
begin                                            
  try
    if openDlg.Execute then
      ExportGridToExcel(openDlg.FileName,cxGrid3,False);
  finally
    MessageDlg('Export Berhasil',mtConfirmation,[mbOk],0);
  end;
end;

procedure TfrmInformasi.Button10Click(Sender: TObject);
begin
  try
    if openDlg.Execute then
      ExportGridToExcel(openDlg.FileName,cxGrid4,False);
  finally
    MessageDlg('Export Berhasil',mtConfirmation,[mbOk],0);
  end;
end;

procedure TfrmInformasi.Button13Click(Sender: TObject);
begin
  try
    if openDlg.Execute then
      ExportGridToExcel(openDlg.FileName,cxGrid5,False);
  finally
    MessageDlg('Export Berhasil',mtConfirmation,[mbOk],0);
  end;
end;

procedure TfrmInformasi.JvThread3Execute(Sender: TObject; Params: Pointer);
var
  qryX:TZQuery;
  qry,kel,qry_insert:string;
  qry_jumlah:string;
  kab,kec,des:string;
  kecprev, desprev, tplkec:string;
  namatps,idtps:string;
  jjmll, jjmlp, jjmls, jtps:integer;
  jjjmll, jjjmlp, jjjmls, jjtps, jdesa:integer;
  jmll, jmlp, jmls: integer;
  tnd:integer;
begin
  cxGrid2DBBandedTableView1.Bands[0].Caption:='REKAP DATA PEMILIH PER TPS '+cbKabkot2.Text;

  tplkec:='';
  desprev:='';
  kecprev:='';

  jjmll :=0;
  jjmlp :=0;
  jjmls :=0;
  jtps :=0;

  jjjmll :=0;
  jjjmlp :=0;
  jjjmls :=0;
  jjtps :=0;
  jdesa :=0;

  //hapus dari rekap_tps;
  qryCek.Active:=False;
  qryCek.SQL.Clear;
  qryCek.SQL.Add('DELETE FROM rekap_tps');
  qryCek.ExecSQL;

  {qryBantu.Active:=False;
  qryBantu.SQL.Clear;
  qryBantu.SQL.Add('SELECT * FROM daftar_tps WHERE LEFT(KODE_DESA,2)='+QuotedStr(kdKab)+' GROUP BY KODE_DESA');
  qryBantu.Active:=True;
  with qryBantu do
  begin
    while not Eof do
    begin
      kel:=fieldByName('KODE_DESA').AsString;
//      ShowMessage(kel);
      qryX:=TZQuery.Create(Self);
      qryX.Connection:=con;
      qryX.Active:=False;
      qryX.SQL.Clear;
      qryX.SQL.Add(('SELECT * FROM information_schema.`TABLES` WHERE table_schema='+QuotedStr(con.Database)+' AND TABLE_NAME='+QuotedStr('pemilih_'+kel)));
      qryX.Active:=True;
//      ShowMessage(IntToStr(qryX.RecordCount));

      if qryX.RecordCount=1 then  //ada tabel
      qryx.Active:=false;
      qryx.SQL.text:= 'TRUNCATE rekap_tps';
      qryx.execSQL;
      begin}

      kab:='';
      kec:='';
      des:='';

      //cari kode desa

      //iterasi utk tiap tps
      qryBantu.Active:=False;
      qryBantu.SQL.Clear;
      qryBantu.SQL.Text:= 'SELECT * FROM daftar_tps WHERE KODE_DESA LIKE '+QuotedStr(kdKab+'%')+'ORDER BY KODE_DESA, NAMA ASC';
      qryBantu.Active:=True;
      qryBantu.First;
      while not qryBantu.Eof do begin
        //cari kabkot kecamatan kelurahan
        kel:=qryBantu.fieldByName('KODE_DESA').AsString;
        qry:='SELECT a.TAMPIL AS NDESA, b.TAMPIL AS NKEC, c.TAMPIL AS NKABKOT FROM (desa a INNER JOIN kecamatan b ON a.KODE_KECAMATAN LIKE b.KODE_KECAMATAN) INNER JOIN kabkot c ON c.KODE = a.KODE_KABKOT WHERE a.KODE_DESA LIKE '+quotedStr(kel);

        //dapet
        qryBantu2.Active:=False;
        qryBantu2.SQL.Clear;
        qryBantu2.sql.text:=qry;
        qryBantu2.Active:=true;
        qryBantu2.First;
        kab := qryBantu2.fieldbyname('NKABKOT').AsString;
        kec := qryBantu2.fieldbyname('NKEC').AsString;
        des := qryBantu2.fieldbyname('NDESA').AsString;
        qryBantu2.Close;

          if(des <> desprev) then begin
            if(desprev <> '') then begin

              if(kec <> kecprev) then
                tplKec := kecprev
              else
                tplkec := kec;

              qry_insert:='INSERT INTO rekap_tps VALUES(NULL, '+quotedstr(kab)+', '+quotedstr(tplkec)+', '+quotedstr('TOTAL '+desprev)+', '+quotedstr(inttostr(jtps))+','+inttostr(jjmls)+','+inttostr(jjmll)+','+inttostr(jjmlp)+')';
              qryX:=TZQuery.Create(Self);
              qryX.Connection:=con;
              with qryX do begin
                active:=false;
                sql.clear;
                sql.Text:=qry_insert;
                execsql;
              end;

              jjjmll := jjjmll + jjmll;
              jjjmlp := jjjmlp + jjmlp;
              jjjmls := jjjmls + jjmls;
              jjtps := jjtps + jtps;
              jdesa := jdesa +1;

              jjmll :=0;
              jjmlp :=0;
              jjmls :=0;
              jtps :=0;

              desprev:=des;

            end else desprev:=des;
          end;
          if(kec <> kecprev) then begin
            if(kecprev <> '') then begin

              qry_insert:='INSERT INTO rekap_tps VALUES(NULL, '+quotedstr(kab)+', '+quotedstr('TOTAL '+kecprev)+', '+quotedstr(inttostr(jdesa))+', '+quotedstr(inttostr(jjtps))+','+inttostr(jjjmls)+','+inttostr(jjjmll)+','+inttostr(jjjmlp)+')';
              qryX:=TZQuery.Create(Self);
              qryX.Connection:=con;
              with qryX do begin
                active:=false;
                sql.clear;
                sql.Text:=qry_insert;
                execsql;
              end;

              jjjmll :=0;
              jjjmlp :=0;
              jjjmls :=0;
              jjtps :=0;
              jdesa :=0;

              kecprev := kec;

            end else kecprev:=kec;
          end;

        //iterasi berdasarkan tps
       { qry:= 'SELECT ID_TPS, NAMA FROM daftar_tps WHERE KODE_DESA LIKE '+quotedstr(kel);
        with QryBantu2 do begin
          Active:=false;
          sql.Clear;
          sql.text:=qry;
          active:=true;
          first;
          while not eof do
          begin}
        idtps := QryBantu.fieldbyname('ID_TPS').asString;
        namatps := QryBantu.fieldbyname('NAMA').asString;
        //showmessage(namatps);
            //cari jumlah
        with qryBantuTps do begin
          qry_jumlah:='SELECT (SELECT COUNT(ID_USER) AS HITUNG FROM pemilih_'+kel+' WHERE TPS = '+idtps+' AND STATUS2 = ''0''),';
          qry_jumlah:=qry_jumlah+' (SELECT COUNT(ID_USER) AS HITUNG FROM pemilih_'+kel+' WHERE KELAMIN = '+QuotedStr('L')+' AND TPS = '+idtps+' AND STATUS2 = ''0''),';
          qry_jumlah:=qry_jumlah+' (SELECT COUNT(ID_USER) AS HITUNG FROM pemilih_'+kel+' WHERE KELAMIN = '+QuotedStr('P')+' AND TPS = '+idtps+' AND STATUS2 = ''0'')';
          active:=false;
          sql.Clear;
          sql.Text:=qry_jumlah;
          tnd:=0;
          active:=true;
          jmls:=0;
          jmll:=0;
          jmlp:=0;
          while not eof do begin


                jmls := Fields[0].AsInteger;

                jmll := Fields[1].AsInteger;

                jmlp := Fields[2].AsInteger;
 
            next;
          end;

          jjmll:=jjmll+jmll;
          jjmlp:=jjmlp+jmlp;
          jjmls:=jjmls+jmls;
          jtps :=jtps+1;

          qry_insert:='INSERT INTO rekap_tps VALUES(NULL, '+quotedstr(kab)+', '+quotedstr(kec)+', '+quotedstr(des)+', '+quotedstr(namatps)+','+inttostr(jmls)+','+inttostr(jmll)+','+inttostr(jmlp)+')';
          qryX:=TZQuery.Create(Self);
          qryX.Connection:=con;
          with qryX do begin
            active:=false;
            sql.clear;
            sql.Text:=qry_insert;
            execsql;
          end;

       end;
            {next;
          end;
        end; }

        {qry:='INSERT INTO rekap_tps SELECT null,kabkot.NAMA AS kabkot,kecamatan.NAMA AS kecamatan,desa.NAMA AS kelurahan,daftar_tps.NAMA,'
          +'(SELECT COUNT(*) FROM pemilih_'+kel+' WHERE TPS=daftar_tps.ID_TPS) AS total,'
          +'(SELECT COUNT(*) FROM pemilih_'+kel+' WHERE TPS=daftar_tps.ID_TPS AND KELAMIN="L") AS Laki,'
          +'(SELECT COUNT(*)FROM pemilih_'+kel+' WHERE TPS=daftar_tps.ID_TPS AND KELAMIN="P") AS perempuan '
          +'FROM daftar_tps,kecamatan,desa,kabkot WHERE daftar_tps.KODE_DESA=desa.KODE_DESA AND '
          +'LEFT(daftar_tps.KODE_DESA,5)=kecamatan.KODE_KECAMATAN AND LEFT(daftar_tps.KODE_DESA,2)='+QuotedStr(kdKab)
          +' AND kabkot.KODE=kecamatan.KODE_KABKOT';}
       { qryBantu2.Active:=False;
        qryBantu2.SQL.Clear;
        qryBantu2.SQL.Add(qry);
//        memo1.Lines.Add(qry);
        qryBantu2.ExecSQL;
      end;
      qryX.Close;
      qryX.Destroy;
      Next;
    end;
  end;  }
       qrybantu.Next;
    end;
    if(des <> '') then begin
              if(kec <> kecprev) then
                tplKec := kecprev
              else
                tplkec := kec;

              qry_insert:='INSERT INTO rekap_tps VALUES(NULL, '+quotedstr(kab)+', '+quotedstr(tplkec)+', '+quotedstr('TOTAL '+desprev)+', '+quotedstr(inttostr(jtps))+','+inttostr(jjmls)+','+inttostr(jjmll)+','+inttostr(jjmlp)+')';
              qryX:=TZQuery.Create(Self);
              qryX.Connection:=con;
              with qryX do begin
                active:=false;
                sql.clear;
                sql.Text:=qry_insert;
                execsql;
              end;

              jjjmll := jjjmll + jjmll;
              jjjmlp := jjjmlp + jjmlp;
              jjjmls := jjjmls + jjmls;
              jjtps := jjtps + jtps;
              jdesa := jdesa +1;

              jjmll :=0;
              jjmlp :=0;
              jjmls :=0;
              jtps :=0;

              desprev:=des;

            end;
            if(kecprev <> '') then begin

              qry_insert:='INSERT INTO rekap_tps VALUES(NULL, '+quotedstr(kab)+', '+quotedstr('TOTAL '+kecprev)+', '+quotedstr(inttostr(jdesa))+', '+quotedstr(inttostr(jjtps))+','+inttostr(jjjmls)+','+inttostr(jjjmll)+','+inttostr(jjjmlp)+')';
              qryX:=TZQuery.Create(Self);
              qryX.Connection:=con;
              with qryX do begin
                active:=false;
                sql.clear;
                sql.Text:=qry_insert;
                execsql;
              end;

              jjjmll :=0;
              jjjmlp :=0;
              jjjmls :=0;
              jjtps :=0;
              jdesa :=0;

              kecprev := kec;

            end;
    cxgrid2.BeginUpdate;
  qryRekapTPS.Active:=False;
  qryRekapTPS.SQL.text:='SELECT * FROM rekap_tps';
  qryRekapTPS.Active:=True;
  cxgrid2.EndUpdate;
end;

procedure TfrmInformasi.JvThread3Begin(Sender: TObject);
begin
WaitProgress1.Active:=True;
end;

procedure TfrmInformasi.JvThread3Finish(Sender: TObject);
begin

WaitProgress1.Active:=False;
end;

procedure TfrmInformasi.Button18Click(Sender: TObject);
begin
  JvThread3.Execute(Self);
end;

procedure TfrmInformasi.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
//  showmessage('hai');
  Action := caFree;
end;

procedure TfrmInformasi.PageControl2Change(Sender: TObject);
begin
  if (pageControl2.ActivePage = tabsheet8) then
    button14.Enabled:=false
  else
    button14.Enabled:=true;
end;

procedure TfrmInformasi.Button1Click(Sender: TObject);

begin
//showmessage('test');
      //showmessage('test2');
if cbKec6.Text<>'' then
  begin
JvThread5.ThreadDialog:=JvThreadSimpleDialog3;
JvThread5.Execute(self);
end
  else
    begin
      //gridTableBand.Bands[0].Caption:='';
      MessageDlg('Kecamatan harus dipilih',mtError,[mbOk],0);
    end;
end;

procedure TfrmInformasi.cbkel6Change(Sender: TObject);
begin
  qryKel.Active:=False;
  qryKel.SQL.Clear;
//  qryKel.SQL.Add('SELECT * FROM desa WHERE TAMPIL='+QuotedStr(cbKel6.Items[cbKel6.ItemIndex])+' AND KODE_KABKOT='+QuotedStr(kdKab)+' AND KODE_KECAMATAN='+QuotedStr(kdKec));
  qryKel.Active:=True;
  kdKel:=qryKel.FieldByName('KODE_DESA').AsString;
end;

procedure TfrmInformasi.Button4Click(Sender: TObject);
begin
  try
    if openDlg.Execute then
      ExportGridToExcel(openDlg.FileName,cxGrid7,False);
  finally
    MessageDlg('Export Berhasil',mtConfirmation,[mbOk],0);
  end;
end;

procedure TfrmInformasi.cbkec6Change(Sender: TObject);
begin
  qryKec.Active:=False;
  qryKec.SQL.Clear;
  qryKec.SQL.Add('SELECT * FROM kecamatan WHERE TAMPIL='+QuotedStr(cbKec6.Items[cbKec6.ItemIndex])+' AND KODE_KABKOT='+QuotedStr(kdKab));
  qryKec.Active:=True;
  kdKec:=qryKec.FieldByName('KODE_KECAMATAN').AsString;

end;

procedure TfrmInformasi.JvThread5Execute(Sender: TObject; Params: Pointer);
var
  textkec, textdes: String;
  sqlbuat:string;
begin
    with cxGridDBBandedTableView1.DataController do
  begin
    BeginUpdate;
    try
      while RecordCount > 0 do
        DeleteRecord(0);
    finally
      EndUpdate;
    end;
    RecordCount:=0;
  end;
  //buat tabel antara dulu
    sqlbuat:='CREATE TABLE IF NOT EXISTS ganda('+
                          'NIK char(36) NOT NULL,'+
                          'NKK char(36) NOT NULL,'+
                          'NAMA char(50) NOT NULL,'+
                          'TEMPAT char(36) NOT NULL,'+
                          'TANGGAL char(10) NOT NULL,'+
                          'IBU char(36) NOT NULL,'+
                          'AGAMA char(36) NOT NULL,'+
                          'KAWIN char(1) NOT NULL,'+
                          'KELAMIN char(1) NOT NULL,'+
                          'KELAINAN char(36) NOT NULL,'+
                          'ALAMAT char(150) NOT NULL,'+
                          'RW char(3) NOT NULL,'+
                          'RT char(3) NOT NULL,'+
                          'DUSUN char(30) NOT NULL,'+
                          'KET char(150) NOT NULL,'+
                          'KECAMATAN char(100) NOT NULL,'+
                          'DESA char(100) NOT NULL)';
    qryAntara.Active:=False;
    qryAntara.SQL.Clear;
    qryAntara.SQL.Text:=sqlbuat;
    qryAntara.ExecSQL;
    qryAntara.Active:=False;
    qryAntara.SQL.Clear;
    qryAntara.SQL.Text:='TRUNCATE ganda';
    qryAntara.ExecSQL;
    {gridTableBand.Bands[0].Caption:='DATA NIK TIDAK SESUAI '+cbKabkot3.Text+', KECAMATAN '+cbKec3.Text+', KELURAHAN/DESA '+cbKel3.Text;}
    qryCek.Active:=False;
    qryCek.SQL.Clear;
    qryCek.SQL.Add('SELECT * FROM information_schema.`TABLES` WHERE table_schema = '+QuotedStr(con.Database)+' AND TABLE_NAME LIKE '+QuotedStr('pemilih_'+kdKec+'%'));
    //showMessage('SELECT * FROM information_schema.`TABLES` WHERE table_schema = '+QuotedStr(con.Database)+' AND TABLE_NAME LIKE '+QuotedStr('pemilih_'+kdKec+'%'));
    qryCek.active:=True;
    //showmessage('SELECT * FROM information_schema.`TABLES` WHERE table_schema = '+QuotedStr(con.Database)+' AND TABLE_NAME LIKE '+QuotedStr('pemilih_'+kdKab+'%'));
    //showmessage(inttostr(qryCek.RecordCount));
    if qryCek.RecordCount>0 then
    begin

      qryCek.First;
      while(not qryCek.Eof) do begin
        //showmessage(qryCek.fieldByName('TABLE_NAME').AsString);
        qryAntara.Active:=False;
        qryAntara.SQL.Clear;
        qryAntara.SQL.Add('SELECT a.TAMPIL, b.TAMPIL FROM desa a INNER JOIN kecamatan b ON a.KODE_KECAMATAN = b.KODE_KECAMATAN WHERE a.KODE_DESA = '+quotedStr(copy(qryCek.fieldByName('TABLE_NAME').AsString,9,8)));
        qryAntara.Active:=True;
        qryAntara.First;
        textdes := qryAntara.Fields[0].AsString;
        textkec := qryAntara.Fields[1].AsString;

        qryAntara.Active:=False;
        qryAntara.SQL.Clear;
        qryAntara.SQL.Add('INSERT INTO ganda SELECT NIK, NKK, NAMA, TEMPAT, TANGGAL, IBU, AGAMA, KAWIN, KELAMIN, KELAINAN, ALAMAT, RW, RT, DUSUN, KET, '+quotedStr(textkec)+', '+quotedStr(textdes)+' FROM '+qryCek.fieldByName('TABLE_NAME').AsString+' WHERE GANDA = 1 AND STATUS2=0 ORDER BY RW, RT, NKK ASC');
        qryAntara.execSql;
        qryCek.Next;
      end;
      qryDagan.Active:=false;
      qryDagan.SQL.Clear;
      qryDagan.SQL.Text:='SELECT * FROM ganda';
      qryDagan.Active:=true;
    end
    else
    begin
      qryDagan.Active:=False;
    end;
end;

procedure TfrmInformasi.JvThread5FinishAll(Sender: TObject);
begin
  sleep(100);
   //DataSource1.DataSet:=zquery3;
   cxGridDBBandedTableView1.BeginUpdate;
   cxGridDBBandedTableView1.DataController.DataSource:=dsDagan;
   cxGridDBBandedTableView1.EndUpdate;
   //cxGridDBBandedTableView1.DataController.CreateAllItems;

   sleep(100);
end;

procedure TfrmInformasi.JvThread4Execute(Sender: TObject; Params: Pointer);
var
  nik:string;
  nama:string;
  where, tabel, qry:string;
  textkec, textdes: String;
  sqlbuat:string;
begin
    with cxGridDBBandedTableView2.DataController do
  begin
    BeginUpdate;
    try
      while RecordCount > 0 do
        DeleteRecord(0);
    finally
      EndUpdate;
    end;
    RecordCount:=0;
  end;

    nik := editnik.Text;
    nama := editnama.Text;
    where := ' WHERE ';
    if(nik <> '') then
      where := where + ' a.NIK LIKE ' +quotedstr('%'+nik+'%')
    else
      where := where + '1';
    if(nama <> '') then
      where := where +' AND a.NAMA LIKE '+ quotedstr('%'+nama+'%');
    {with qryCari do begin
      active:=false;
      sql.Clear;
      sql.Add();
    end;}
//showmessage('test');
      //showmessage('test2');
//if cbKel6.Text<>'' then
  //begin
  //buat tabel antara dulu
    sqlbuat:='CREATE TABLE IF NOT EXISTS cari('+
                          'NIK char(36) NOT NULL,'+
                          'NKK char(36) NOT NULL,'+
                          'NAMA char(50) NOT NULL,'+
                          'TEMPAT char(36) NOT NULL,'+
                          'TANGGAL char(10) NOT NULL,'+
                          'IBU char(36) NOT NULL,'+
                          'AGAMA char(36) NOT NULL,'+
                          'KAWIN char(1) NOT NULL,'+
                          'KELAMIN char(1) NOT NULL,'+
                          'KELAINAN char(36) NOT NULL,'+
                          'ALAMAT char(150) NOT NULL,'+
                          'RW char(3) NOT NULL,'+
                          'RT char(3) NOT NULL,'+
                          'DUSUN char(30) NOT NULL,'+
                          'KET char(150) NOT NULL,'+
                          'TPS char(3) NOT NULL,'+
                          'KECAMATAN char(100) NOT NULL,'+
                          'DESA char(100) NOT NULL)';
    qryAntara.Active:=False;
    qryAntara.SQL.Clear;
    qryAntara.SQL.Text:=sqlbuat;
    qryAntara.ExecSQL;
    qryAntara.Active:=False;
    qryAntara.SQL.Clear;
    qryAntara.SQL.Text:='TRUNCATE cari';
    qryAntara.ExecSQL;
    {gridTableBand.Bands[0].Caption:='DATA NIK TIDAK SESUAI '+cbKabkot3.Text+', KECAMATAN '+cbKec3.Text+', KELURAHAN/DESA '+cbKel3.Text;}
    qryCek.Active:=False;
    qryCek.SQL.Clear;
    qryCek.SQL.Add('SELECT * FROM information_schema.`TABLES` WHERE table_schema = '+QuotedStr(con.Database)+' AND TABLE_NAME LIKE '+QuotedStr('pemilih_'+kdKab+'%'));
    qryCek.active:=True;
    //showmessage('SELECT * FROM information_schema.`TABLES` WHERE table_schema = '+QuotedStr(con.Database)+' AND TABLE_NAME LIKE '+QuotedStr('pemilih_'+kdKab+'%'));
    //showmessage(inttostr(qryCek.RecordCount));
    if qryCek.RecordCount>0 then
    begin

      qryCek.First;
      while(not qryCek.Eof) do begin
        //showmessage(qryCek.fieldByName('TABLE_NAME').AsString);
        qryAntara.Active:=False;
        qryAntara.SQL.Clear;
        qryAntara.SQL.Add('SELECT a.TAMPIL, b.TAMPIL FROM desa a INNER JOIN kecamatan b ON a.KODE_KECAMATAN = b.KODE_KECAMATAN WHERE a.KODE_DESA = '+quotedStr(copy(qryCek.fieldByName('TABLE_NAME').AsString,9,8)));
        qryAntara.Active:=True;
        qryAntara.First;
        textdes := qryAntara.Fields[0].AsString;
        textkec := qryAntara.Fields[1].AsString;

        qryAntara.Active:=False;
        qryAntara.SQL.Clear;
        qryAntara.SQL.Add('INSERT INTO cari SELECT a.NIK, a.NKK, a.NAMA, a.TEMPAT, a.TANGGAL, a.IBU, a.AGAMA, a.KAWIN, a.KELAMIN, a.KELAINAN, a.ALAMAT, a.RW, a.RT, a.DUSUN, a.KET,b.NAMA, '+quotedStr(textkec)+', '+quotedStr(textdes)+' FROM ('+qryCek.fieldByName('TABLE_NAME').AsString+' a LEFT JOIN daftar_tps b ON a.TPS = b.ID_TPS)'+where+' AND STATUS2=0 ORDER BY RW, RT, NKK ASC');
        qryAntara.execSql;
        qryCek.Next;
      end;
      qryCari.Active:=false;
      qryCari.SQL.Clear;
      qryCari.SQL.Text:='SELECT * FROM cari';
      qryCari.Active:=true;
    end
    else
    begin
      qryCari.Active:=False;
    end;
  //showmessage('beres');


end;

procedure TfrmInformasi.JvThread4FinishAll(Sender: TObject);
begin
  sleep(100);
   //DataSource1.DataSet:=zquery3;
   cxGridDBBandedTableView2.beginUpdate;
   cxGridDBBandedTableView2.DataController.DataSource:=dsCari;
   cxGridDBBandedTableView2.endupdate;
//   cxGridDBBandedTableView2.DataController.CreateAllItems;

   sleep(100);
end;

procedure TfrmInformasi.Button6Click(Sender: TObject);
begin
if ((editnik.Text = '') and (editnama.text = '')) then
  begin
    showmessage('Silakan isi NIK/Nama yang akan dicari terlebih dahulu');
  end
  else
  begin
JvThread4.ThreadDialog:=JvThreadSimpleDialog2;
JvThread4.Execute(self);
end;
end;

procedure TfrmInformasi.JvThread2FinishAll(Sender: TObject);
begin
  cxGrid1DBBandedTableView1.BeginUpdate;
  cxGrid1DBBandedTableView1.DataController.DataSource := dsRekapDataPemilih;
  cxGrid1DBBandedTableView1.EndUpdate;
end;

procedure TfrmInformasi.JvThread3FinishAll(Sender: TObject);
begin
  cxGrid2DBBandedTableView1.BeginUpdate;
  cxGrid2DBBandedTableView1.DataController.DataSource := dsRekapTPS;
  cxGrid2DBBandedTableView1.EndUpdate;
end;

procedure TfrmInformasi.Button9Click(Sender: TObject);
begin
  threadKelUmur.ThreadDialog := threadDialogKelUmur;
  threadKelUmur.Execute(Self);  
end;

procedure TfrmInformasi.ThreadKelUmurExecute(Sender: TObject;
  Params: Pointer);
var
  sqlAwal, sqlCari, sqlTanggal, sqlCreate, sqlTruncate, sqlInsert : String;
  tglAcuan, kodes, kokec, tades, takec, takab : String;
  no, umur, kel1, kel2, kel3, kel4, kel5, kel6, kel7, kel8, kel9, kel10, total : Integer;
begin
  //bikin tabel jika belom ada
  sqlCreate:= 'CREATE TABLE IF NOT EXISTS `rekap_kelompok_umur` ('
  +'`no` int(11) NOT NULL,'
  +'`kabkot` char(40) NOT NULL,'
  +'`kec` char(40) NOT NULL,'
  +'`desa` char(40) NOT NULL,'
  +'`kel1` int(11) NOT NULL COMMENT ''~17'','
  +'`kel2` int(11) NOT NULL COMMENT ''17-21'','
  +'`kel3` int(11) NOT NULL COMMENT ''22-26'','
  +'`kel4` int(11) NOT NULL COMMENT ''27-31'','
  +'`kel5` int(11) NOT NULL COMMENT ''32-36'','
  +'`kel6` int(11) NOT NULL COMMENT ''37-41'','
  +'`kel7` int(11) NOT NULL COMMENT ''42-46'','
  +'`kel8` int(11) NOT NULL COMMENT ''47-51'','
  +'`kel9` int(11) NOT NULL COMMENT ''52-56'','
  +'`kel10` int(11) NOT NULL COMMENT ''57~'','
  +'`total` int(11) NOT NULL'
  +') ENGINE=MyISAM DEFAULT CHARSET=latin1;';

  qryCreate.Active:=false;
  qryCreate.SQL.Clear;
  qryCreate.SQL.Text:= sqlCreate;
  qryCreate.ExecSQL;

  //truncate dulu
  sqlTruncate:='TRUNCATE rekap_kelompok_umur';
  qryCreate.Active:=false;
  qryCreate.SQL.Clear;
  qryCreate.SQL.Text:= sqlTruncate;
  qryCreate.ExecSQL;

  //ambil tanggal pilgub
  sqlTanggal := 'SELECT DATE_FORMAT(TANGGAL, ''%Y-%m-%d'') AS TANGGALS FROM info_tanggal WHERE AKTIF = 1';
  qryTanggal.Active := false;
  qryTanggal.SQL.Clear;
  qryTanggal.SQL.Text := sqlTanggal;
  qryTanggal.Active := true;
  while not qryTanggal.Eof do begin
    tglAcuan := qryTanggal.fieldByName('TANGGALS').AsString;
    qryTanggal.Next;
  end;
  //showmessage(tglAcuan);

  //cari kota
  sqlAwal := 'SELECT a.KODE_DESA AS KODES, a.TAMPIL AS NDESA, b.KODE_KECAMATAN AS KOKEC, b.TAMPIL AS NKEC, c.TAMPIL AS NKABKOT FROM (desa a INNER JOIN kecamatan b ON a.KODE_KECAMATAN LIKE b.KODE_KECAMATAN)'+' INNER JOIN kabkot c ON c.KODE = a.KODE_KABKOT WHERE a.KODE_KABKOT LIKE '+quotedStr(skota)+' ORDER BY a.KODE_DESA';
  //showmessage(sqlAwal);
  no:=1;
  qryWil.active := false;
  qryWil.SQL.Clear;
  qryWil.SQL.Text := sqlAwal;
  qryWil.Active := true;
  qryWil.First;
  while not qryWil.Eof do begin
    kodes := qryWil.FieldByName('KODES').AsString;
    tades := qryWil.FieldByName('NDESA').AsString;
    takec := qryWil.FieldByName('NKEC').AsString;
    takab := qryWil.FieldByName('NKABKOT').AsString;
    
    //showmessage(kodes);
    qry9.active:=false;
    qry9.SQL.Clear;
    qry9.SQL.Text := 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '+quotedStr(con.Database)+' AND TABLE_NAME = '+quotedStr('pemilih_'+kodes);
    qry9.Active:=true;
    if(qry9.RecordCount >= 1) then begin
      sqlCari := 'SELECT DATE_FORMAT(FROM_DAYS(TO_DAYS('+quotedStr(tglAcuan)+')-TO_DAYS(CONCAT(RIGHT(TANGGAL,4),''-'',MID(TANGGAL,4,2),''-'',LEFT(TANGGAL,2)))), ''%Y'')+0 AS UMUR '
      +' FROM pemilih_'+kodes
      +' WHERE STATUS2 = '+quotedStr('0');
      qry8.Active:=false;
      qry8.SQL.Clear;
      qry8.SQL.Text:=sqlCari;
      qry8.Active:=true;
      qry8.First;
      total := 0;
      kel1 := 0;
      kel2 := 0;
      kel3 := 0;
      kel4 := 0;
      kel5 := 0;
      kel6 := 0;
      kel7 := 0;
      kel8 := 0;
      kel9 := 0;
      kel10 := 0;

      while not qry8.Eof do begin
        umur := qry8.fieldByName('UMUR').AsInteger;
        //ambil kelompok umur
        if(umur < 17) then
          kel1:=kel1+1
        else if((umur >= 17) and (umur < 22)) then
          kel2:=kel2+1
        else if((umur >= 22) and (umur < 27)) then
          kel3:=kel3+1
        else if((umur >= 27) and (umur < 32)) then
          kel4:=kel4+1
        else if((umur >= 32) and (umur < 37)) then
          kel5:=kel5+1
        else if((umur >= 37) and (umur < 42)) then
          kel6:=kel6+1
        else if((umur >= 42) and (umur < 47)) then
          kel7:=kel7+1
        else if((umur >= 47) and (umur < 52)) then
          kel8:=kel8+1
        else if((umur >= 52) and (umur < 57)) then
          kel9:=kel9+1
        else
          kel10:=kel10+1;
        total := total+1;
        qry8.Next;
      end;
      //showmessage(inttostr(total));
      //masukin ke database
      sqlInsert:= 'INSERT INTO rekap_kelompok_umur VALUES ('
      +quotedStr(inttostr(no))+','
      +quotedStr(takab)+','
      +quotedStr(takec)+','
      +quotedStr(tades)+','
      +quotedStr(inttostr(kel1))+','
      +quotedStr(inttostr(kel2))+','
      +quotedStr(inttostr(kel3))+','
      +quotedStr(inttostr(kel4))+','
      +quotedStr(inttostr(kel5))+','
      +quotedStr(inttostr(kel6))+','
      +quotedStr(inttostr(kel7))+','
      +quotedStr(inttostr(kel8))+','
      +quotedStr(inttostr(kel9))+','
      +quotedStr(inttostr(kel10))+','
      +quotedStr(inttostr(total))
      +')';

      qry8.Active:=false;
      qry8.SQL.Clear;
      qry8.SQL.Text:=sqlInsert;
      qry8.execSQL;

      no:=no+1;
    end;
    qryWil.Next;
  end;
end;

procedure TfrmInformasi.ThreadKelUmurFinishAll(Sender: TObject);
begin
  qryKelUmur.Active:=false;
  qryKelUmur.SQL.Clear;
  qryKelUmur.SQL.Text:='SELECT * FROM rekap_kelompok_umur';
  qryKelUmur.Active:=true;
  //dsKelUmur.DataSet:=qryKelUmur;

  cxGrid9.BeginUpdate;
  cxGridDBBandedTableView3.DataController.DataSource:=dsKelUmur;
  cxGrid9.EndUpdate;
end;

procedure TfrmInformasi.Button12Click(Sender: TObject);
begin
  try
    if openDlg.Execute then
      ExportGridToExcel(openDlg.FileName,cxGrid9,False);
  finally
    MessageDlg('Export Berhasil',mtConfirmation,[mbOk],0);
  end;
end;

procedure TfrmInformasi.Button16Click(Sender: TObject);
begin
  try
    if openDlg.Execute then
      ExportGridToExcel(openDlg.FileName,cxGrid10,False);
  finally
    MessageDlg('Export Berhasil',mtConfirmation,[mbOk],0);
  end;
end;

procedure TfrmInformasi.Button15Click(Sender: TObject);

begin
  threadTPS.ThreadDialog:= threadDialogTPS;
  threadTPS.Execute(Self);
  
end;

procedure TfrmInformasi.ThreadTPSExecute(Sender: TObject; Params: Pointer);
var
  sqlAwal, sqlCari, sqlTanggal, sqlCreate, sqlTruncate, sqlInsert : String;
  tglAcuan, kodes, kokec, tades, takec, takab : String;
  no, total : integer;
begin
  //bikin tabel jika belom ada
  sqlCreate:= 'CREATE TABLE IF NOT EXISTS `rekap_jumlah_tps` ('
  +'`no` int(11) NOT NULL,'
  +'`kabkot` char(40) NOT NULL,'
  +'`kec` char(40) NOT NULL,'
  +'`desa` char(40) NOT NULL,'
  +'`jumlah_tps` int(11) NOT NULL'
  +') ENGINE=MyISAM DEFAULT CHARSET=latin1;';

  qryCreate.Active:=false;
  qryCreate.SQL.Clear;
  qryCreate.SQL.Text:= sqlCreate;
  qryCreate.ExecSQL;

  //truncate dulu
  sqlTruncate:='TRUNCATE rekap_jumlah_tps';
  qryCreate.Active:=false;
  qryCreate.SQL.Clear;
  qryCreate.SQL.Text:= sqlTruncate;
  qryCreate.ExecSQL;

  //cari kota
  sqlAwal := 'SELECT a.KODE_DESA AS KODES, a.TAMPIL AS NDESA, b.KODE_KECAMATAN AS KOKEC, b.TAMPIL AS NKEC, c.TAMPIL AS NKABKOT FROM (desa a INNER JOIN kecamatan b ON a.KODE_KECAMATAN LIKE b.KODE_KECAMATAN)'+' INNER JOIN kabkot c ON c.KODE = a.KODE_KABKOT WHERE a.KODE_KABKOT LIKE '+quotedStr(skota);
  //showmessage(sqlAwal);
  no:=1;
  qryWil.active := false;
  qryWil.SQL.Clear;
  qryWil.SQL.Text := sqlAwal;
  qryWil.Active := true;
  qryWil.First;
  while not qryWil.Eof do begin
    kodes := qryWil.FieldByName('KODES').AsString;
    tades := qryWil.FieldByName('NDESA').AsString;
    takec := qryWil.FieldByName('NKEC').AsString;
    takab := qryWil.FieldByName('NKABKOT').AsString;
    //showmessage(kodes);
    qry9.active:=false;
    qry9.SQL.Clear;
    qry9.SQL.Text := 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '+quotedStr(con.Database)+' AND TABLE_NAME = '+quotedStr('pemilih_'+kodes);
    qry9.Active:=true;
    if(qry9.RecordCount >= 1) then begin
      sqlCari := 'SELECT COUNT(ID_TPS) AS JUMLAH '
      +' FROM daftar_tps'
      +' WHERE KODE_DESA = '+quotedStr(kodes);
      qry8.Active:=false;
      qry8.SQL.Clear;
      qry8.SQL.Text:=sqlCari;
      qry8.Active:=true;
      qry8.First;
      total := 0;

      while not qry8.Eof do begin
        total := qry8.fieldByName('JUMLAH').AsInteger;
        qry8.Next;
      end;
      //showmessage(inttostr(total));
    end;

    //masukin ke database
    sqlInsert:= 'INSERT INTO rekap_jumlah_tps VALUES ('
    +quotedStr(inttostr(no))+','
    +quotedStr(takab)+','
    +quotedStr(takec)+','
    +quotedStr(tades)+','
    +quotedStr(inttostr(total))
    +')';

    qry8.Active:=false;
    qry8.SQL.Clear;
    qry8.SQL.Text:=sqlInsert;
    qry8.execSQL;

    no:=no+1;
    qryWil.Next;
  end;
end;

procedure TfrmInformasi.ThreadTPSFinishAll(Sender: TObject);
begin
  qryJmlTPS.Active:=false;
  qryJmlTPS.SQL.Clear;
  qryJmlTPS.SQL.Text:='SELECT * FROM rekap_jumlah_tps';
  qryJmlTPS.Active:=true;
  //dsKelUmur.DataSet:=qryKelUmur;

  cxGrid10.BeginUpdate;
  cxGridDBBandedTableView4.DataController.DataSource:=dsJmlTPS;
  cxGrid10.EndUpdate;
end;

procedure TfrmInformasi.Button20Click(Sender: TObject);
begin
  try
    if openDlg.Execute then
      ExportGridToExcel(openDlg.FileName,cxGrid11,False);
  finally
    MessageDlg('Export Berhasil',mtConfirmation,[mbOk],0);
  end;
end;

procedure TfrmInformasi.Button19Click(Sender: TObject);
var
  sqlCreate, sqlTruncate, sqlInsert, sqlCek, kodes:string;
  sqlRW, sqlRT, sqlJum, curRW, curRT :string;
  no, jml, jmll, jmlp : integer;
begin
  if cbKelRT.Text<>'' then
  begin
    threadRT.ThreadDialog:=threadDialogRT;
    threadRT.Execute(Self);

  end
  else
  begin
    //cxGrid5DBBandedTableView1.Bands[0].Caption:='';
    MessageDlg('Kecamatan dan Kelurahan harus dipilih',mtError,[mbOk],0);
  end;
end;

procedure TfrmInformasi.threadRTExecute(Sender: TObject; Params: Pointer);
var
  sqlCreate, sqlTruncate, sqlInsert, sqlCek, kodes:string;
  sqlRW, sqlRT, sqlJum, curRW, curRT :string;
  no, jml, jmll, jmlp : integer;
begin
  //bikin tabel jika belom ada
    sqlCreate:= 'CREATE TABLE IF NOT EXISTS `rekap_pemilih_rt` ('
    +'`no` int(11) NOT NULL,'
    +'`rw` char(3) NOT NULL,'
    +'`rt` char(3) NOT NULL,'
    +'`jumlah` int(11) NOT NULL,'
    +'`jumlah_laki` int(11) NOT NULL,'
    +'`jumlah_pr` int(11) NOT NULL'
    +') ENGINE=MyISAM DEFAULT CHARSET=latin1;';

    qryCreate.Active:=false;
    qryCreate.SQL.Clear;
    qryCreate.SQL.Text:= sqlCreate;
    qryCreate.ExecSQL;

    //truncate dulu
    sqlTruncate:='TRUNCATE rekap_pemilih_rt';
    qryCreate.Active:=false;
    qryCreate.SQL.Clear;
    qryCreate.SQL.Text:= sqlTruncate;
    qryCreate.ExecSQL;
    no:= 1;
    kodes := kdKel;
    sqlCek := 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '
            +quotedStr(con.Database)+' AND TABLE_NAME = '+quotedStr('pemilih_'+kodes);

    qryWil.Active:=false;
    qryWil.SQL.Clear;
    qryWil.SQL.Text:=sqlCek;
    qryWil.Active:=true;
    //showmessage(sqlCek);
    if(qryWil.RecordCount >= 1) then begin
      //cari RW
      sqlRW := 'SELECT DISTINCT(RW) AS RWS FROM pemilih_'+kodes+' WHERE STATUS2 = ''0'' ORDER BY RW';
      qry1.Active:=false;
      qry1.SQL.Clear;
      qry1.SQL.Text:=sqlRW;
      qry1.Active:=true;
      qry1.First;
      while not qry1.Eof do begin
        curRW:= qry1.fieldbyname('RWS').AsString;
        //cari RT
        sqlRT := 'SELECT DISTINCT(RT) AS RTS FROM pemilih_'+kodes+' WHERE RW = '+quotedStr(curRW)+' AND STATUS2 = ''0'' ORDER BY RT';
        qry2.Active:=false;
        qry2.SQL.Clear;
        qry2.SQL.Text:=sqlRT;
        qry2.Active:=true;
        qry2.First;
        while not qry2.Eof do begin
          curRT:= qry2.fieldbyname('RTS').AsString;
          //cari jumlah masing-masing RT
          sqlJum := 'SELECT KELAMIN FROM pemilih_'+kodes+' WHERE RW = '+quotedStr(curRW)+' AND STATUS2 = ''0'' AND RT = '+quotedStr(curRT);
          qry3.Active:=false;
          qry3.SQL.Clear;
          qry3.SQL.Text:=sqlJum;
          qry3.Active:=true;
          qry3.First;
          jml := 0;
          jmll := 0;
          jmlp := 0;
          while not qry3.Eof do begin
            jml := jml+1;
            if(qry3.FieldByName('KELAMIN').AsString = 'L') then
              jmll := jmll+1
            else
              jmlp := jmlp+1;
            qry3.Next;
          end;
          sqlInsert := 'INSERT INTO rekap_pemilih_rt VALUES('
            +quotedStr(inttostr(no))+','
            +quotedStr(curRW)+','
            +quotedStr(curRT)+','
            +quotedStr(inttostr(jml))+','
            +quotedStr(inttostr(jmll))+','
            +quotedStr(inttostr(jmlp))+''
            +')';
          qry4.Active :=false;
          qry4.SQL.Clear;
          qry4.SQL.Text := sqlInsert;
          qry4.ExecSQL;
          qry2.Next;
          no:= no+1;
        end;
        qry1.Next;
      end;
    end;
end;

procedure TfrmInformasi.threadRTFinishAll(Sender: TObject);
begin
  qryRT.Active:=false;
  qryRT.SQL.Clear;
  qryRT.SQL.Text:='SELECT * FROM rekap_pemilih_rt';
  qryRT.Active:=true;

  cxGrid11.BeginUpdate;
  cxGridDBBandedTableView5.Bands[0].Caption := 'REKAP PEMILIH PER RT';
  cxGridDBBandedTableView5.DataController.DataSource := dsRT;
  cxGrid11.EndUpdate;
end;

procedure TfrmInformasi.cbKabKotRTChange(Sender: TObject);
begin
  qryKabKot.Active:=False;
  qryKabKot.SQL.Clear;
  qryKabKot.SQL.Add('SELECT * FROM kabkot WHERE TAMPIL='+QuotedStr(cbKabkotRT.Items[cbKabkotRT.ItemIndex]));
  qryKabKot.Active:=True;
  kdKab:=qryKabKot.FieldByName('KODE').AsString;

  qryKec.Active:=False;
  qryKec.SQL.Clear;
  qryKec.SQL.Add('SELECT * FROM kecamatan WHERE KODE_KABKOT='+QuotedStr(kdKab));
  qryKec.Active:=True;

  cbKecRT.Items.Clear;
  while not qryKec.Eof do
  begin
    cbKecRT.Items.Add(qryKec.FieldByName('TAMPIL').AsString);
    qryKec.Next;
  end;
end;

procedure TfrmInformasi.cbKecRTChange(Sender: TObject);
begin
  qryKec.Active:=False;
  qryKec.SQL.Clear;
  qryKec.SQL.Add('SELECT * FROM kecamatan WHERE TAMPIL='+QuotedStr(cbKecRT.Items[cbKecRT.ItemIndex])+' AND KODE_KABKOT='+QuotedStr(kdKab));
  qryKec.Active:=True;
  kdKec:=qryKec.FieldByName('KODE_KECAMATAN').AsString;

  qryKel.Active:=False;
  qryKel.SQL.Clear;
  qryKel.SQL.Add('SELECT * FROM desa WHERE KODE_KABKOT='+QuotedStr(kdKab)+' AND KODE_KECAMATAN='+QuotedStr(kdKec));
  qryKel.Active:=True;

  cbKelRT.Items.Clear;
  while not qryKel.Eof do
  begin
    cbKelRT.Items.Add(qryKel.FieldByName('TAMPIL').AsString);
    qryKel.Next;
  end;
end;

procedure TfrmInformasi.cbKelRTChange(Sender: TObject);
begin
  qryKel.Active:=False;
  qryKel.SQL.Clear;
  qryKel.SQL.Add('SELECT * FROM desa WHERE TAMPIL='+QuotedStr(cbKelRT.Items[cbKelRT.ItemIndex])+' AND KODE_KABKOT='+QuotedStr(kdKab)+' AND KODE_KECAMATAN='+QuotedStr(kdKec));
  qryKel.Active:=True;
  kdKel:=qryKel.FieldByName('KODE_DESA').AsString;
end;

procedure TfrmInformasi.ThreadHapusExecute(Sender: TObject;
  Params: Pointer);
var
  sqlAwal, sqlCari, sqlTanggal, sqlCreate, sqlTruncate, sqlInsert : String;
  tglAcuan, kodes, kokec, tades, takec, takab, status2 : String;
  no, umur, kel1, kel2, kel3, kel4, kel5, kel6, total : Integer;
begin
  //bikin tabel jika belom ada
  sqlCreate:= 'CREATE TABLE IF NOT EXISTS `rekap_pemilih_hapus` ('
  +'`no` int(11) NOT NULL,'
  +'`kabkot` char(40) NOT NULL,'
  +'`kec` char(40) NOT NULL,'
  +'`desa` char(40) NOT NULL,'
  +'`kel1` int(11) NOT NULL COMMENT ''Meninggal Dunia'','
  +'`kel2` int(11) NOT NULL COMMENT ''Data Fiktif'','
  +'`kel3` int(11) NOT NULL COMMENT ''TNI/Polri'','
  +'`kel4` int(11) NOT NULL COMMENT ''Pindah Alamat'','
  +'`kel5` int(11) NOT NULL COMMENT ''Terdaftar Ganda'','
  +'`kel6` int(11) NOT NULL COMMENT ''Lain-lain'','
  +'`total` int(11) NOT NULL'
  +') ENGINE=MyISAM DEFAULT CHARSET=latin1;';

  qryCreate.Active:=false;
  qryCreate.SQL.Clear;
  qryCreate.SQL.Text:= sqlCreate;
  qryCreate.ExecSQL;

  //truncate dulu
  sqlTruncate:='TRUNCATE rekap_pemilih_hapus';
  qryCreate.Active:=false;
  qryCreate.SQL.Clear;
  qryCreate.SQL.Text:= sqlTruncate;
  qryCreate.ExecSQL;

  //ambil tanggal pilgub
  sqlTanggal := 'SELECT DATE_FORMAT(TANGGAL, ''%Y-%m-%d'') AS TANGGALS FROM info_tanggal WHERE AKTIF = 1';
  qryTanggal.Active := false;
  qryTanggal.SQL.Clear;
  qryTanggal.SQL.Text := sqlTanggal;
  qryTanggal.Active := true;
  while not qryTanggal.Eof do begin
    tglAcuan := qryTanggal.fieldByName('TANGGALS').AsString;
    qryTanggal.Next;
  end;
  //showmessage(tglAcuan);

  //cari kota
  sqlAwal := 'SELECT a.KODE_DESA AS KODES, a.TAMPIL AS NDESA, b.KODE_KECAMATAN AS KOKEC, b.TAMPIL AS NKEC, c.TAMPIL AS NKABKOT FROM (desa a INNER JOIN kecamatan b ON a.KODE_KECAMATAN LIKE b.KODE_KECAMATAN)'+' INNER JOIN kabkot c ON c.KODE = a.KODE_KABKOT WHERE a.KODE_KABKOT LIKE '+quotedStr(skota);
  //showmessage(sqlAwal);
  no:=1;
  qryWil.active := false;
  qryWil.SQL.Clear;
  qryWil.SQL.Text := sqlAwal;
  qryWil.Active := true;
  qryWil.First;
  while not qryWil.Eof do begin
    kodes := qryWil.FieldByName('KODES').AsString;
    tades := qryWil.FieldByName('NDESA').AsString;
    takec := qryWil.FieldByName('NKEC').AsString;
    takab := qryWil.FieldByName('NKABKOT').AsString;
    //showmessage(kodes);
    qry9.active:=false;
    qry9.SQL.Clear;
    qry9.SQL.Text := 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '+quotedStr(con.Database)+' AND TABLE_NAME = '+quotedStr('pemilih_'+kodes);
    qry9.Active:=true;
    if(qry9.RecordCount >= 1) then begin
      sqlCari := 'SELECT STATUS2 '
      +' FROM pemilih_'+kodes
      +' WHERE STATUS2 <> '+quotedStr('0');
      qry8.Active:=false;
      qry8.SQL.Clear;
      qry8.SQL.Text:=sqlCari;
      qry8.Active:=true;
      qry8.First;
      total := 0;
      kel1 := 0;
      kel2 := 0;
      kel3 := 0;
      kel4 := 0;
      kel5 := 0;
      kel6 := 0;

      while not qry8.Eof do begin
        status2 := qry8.fieldByName('STATUS2').AsString;
        //ambil kelompok umur
        if(status2 = '1') then
          kel1:=kel1+1
        else if(status2 = '2') then
          kel2:=kel2+1
        else if(status2 = '3') then
          kel3:=kel3+1
        else if(status2 = '4') then
          kel4:=kel4+1
        else if(status2 = '5') then
          kel5:=kel5+1
        else if(status2 = '6') then
          kel6:=kel6+1;
        total := total+1;
        qry8.Next;
      end;
      //masukin ke database
      sqlInsert:= 'INSERT INTO rekap_pemilih_hapus VALUES ('
      +quotedStr(inttostr(no))+','
      +quotedStr(takab)+','
      +quotedStr(takec)+','
      +quotedStr(tades)+','
      +quotedStr(inttostr(kel1))+','
      +quotedStr(inttostr(kel2))+','
      +quotedStr(inttostr(kel3))+','
      +quotedStr(inttostr(kel4))+','
      +quotedStr(inttostr(kel5))+','
      +quotedStr(inttostr(kel6))+','
      +quotedStr(inttostr(total))
      +')';

      qry8.Active:=false;
      qry8.SQL.Clear;
      qry8.SQL.Text:=sqlInsert;
      qry8.execSQL;

      no:=no+1;
      //showmessage(inttostr(total));
    end;
    qryWil.Next;
  end;
end;

procedure TfrmInformasi.ThreadHapusFinishAll(Sender: TObject);
begin
  qryHapus.Active:=false;
  qryHapus.SQL.Clear;
  qryHapus.SQL.Text:='SELECT * FROM rekap_pemilih_hapus';
  qryHapus.Active:=true;
  //dsKelUmur.DataSet:=qryKelUmur;

  cxGrid12.BeginUpdate;
  cxGridDBBandedTableView6.DataController.DataSource:=dsHapus;
  cxGrid12.EndUpdate;
end;

procedure TfrmInformasi.Button21Click(Sender: TObject);
begin
  threadHapus.ThreadDialog := threadDialogHapus;
  threadHapus.Execute(Self);
end;

procedure TfrmInformasi.Button22Click(Sender: TObject);
begin
  try
    if openDlg.Execute then
      ExportGridToExcel(openDlg.FileName,cxGrid12,False);
  finally
    MessageDlg('Export Berhasil',mtConfirmation,[mbOk],0);
  end;
end;

procedure TfrmInformasi.Button24Click(Sender: TObject);
begin
  try
    if openDlg.Execute then
      ExportGridToExcel(openDlg.FileName,cxGrid13,False);
  finally
    MessageDlg('Export Berhasil',mtConfirmation,[mbOk],0);
  end;
end;

procedure TfrmInformasi.Button23Click(Sender: TObject);
begin
  threadCacat.ThreadDialog := threadDialogCacat;
  threadCacat.Execute(Self);
end;

procedure TfrmInformasi.threadCacatExecute(Sender: TObject;
  Params: Pointer);
var
  sql_wilayah, sql_cek, sql_tps, sql_pemilih, sql_create, sql_truncate, sql_insert:String;
  kodes, kokec, tades, takec, takab, cacat, idtps, natps,hasil : String;
  no, kel1, kel2, kel3, kel4, kel5, kel6, totaltps : integer;
  tkel1, tkel2, tkel3, tkel4, tkel5, tkel6: integer;
begin
  sql_create:= 'CREATE TABLE IF NOT EXISTS `rekap_pemilih_cacat` ('
  +'`no` int(11) NOT NULL,'
  +'`kabkot` char(40) NOT NULL,'
  +'`kec` char(40) NOT NULL,'
  +'`desa` char(40) NOT NULL,'
  +'`tps` char(40) NOT NULL,'
  +'`kel1` int(11) NOT NULL COMMENT ''Cacat Fisik'','
  +'`kel2` int(11) NOT NULL COMMENT ''Netra/Buta'','
  +'`kel3` int(11) NOT NULL COMMENT ''Rungu Wicara'','
  +'`kel4` int(11) NOT NULL COMMENT ''Mental Jiwa'','
  +'`kel5` int(11) NOT NULL COMMENT ''Fisik/Mental'','
  +'`kel6` int(11) NOT NULL COMMENT ''Lainnya'''
  +') ENGINE=MyISAM DEFAULT CHARSET=latin1;';

  qryCreate.Active:=false;
  qryCreate.SQL.Clear;
  qryCreate.SQL.Text:= sql_create;
  qryCreate.ExecSQL;

  //truncate dulu
  sql_truncate:='TRUNCATE rekap_pemilih_cacat';
  qryCreate.Active:=false;
  qryCreate.SQL.Clear;
  qryCreate.SQL.Text:= sql_truncate;
  qryCreate.ExecSQL;

  //wilayah
  //cari kota
  sql_wilayah := 'SELECT a.KODE_DESA AS KODES, a.TAMPIL AS NDESA, b.KODE_KECAMATAN AS KOKEC, b.TAMPIL AS NKEC, c.TAMPIL AS NKABKOT FROM (desa a INNER JOIN kecamatan b ON a.KODE_KECAMATAN LIKE b.KODE_KECAMATAN)'+' INNER JOIN kabkot c ON c.KODE = a.KODE_KABKOT WHERE a.KODE_KABKOT LIKE '+quotedStr(skota);
  //showmessage(sqlAwal);
  no:=1;
  qryWil.active := false;
  qryWil.SQL.Clear;
  qryWil.SQL.Text := sql_wilayah;
  qryWil.Active := true;
  qryWil.First;
  while not qryWil.Eof do begin
    kokec := qryWil.FieldByName('KOKEC').AsString;
    kodes := qryWil.FieldByName('KODES').AsString;
    tades := qryWil.FieldByName('NDESA').AsString;
    takec := qryWil.FieldByName('NKEC').AsString;
    takab := qryWil.FieldByName('NKABKOT').AsString;

    tkel1:=0;
    tkel2:=0;
    tkel3:=0;
    tkel4:=0;
    tkel5:=0;
    tkel6:=0;

    qry9.active:=false;
    qry9.SQL.Clear;
    qry9.SQL.Text := 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '+quotedStr(con.Database)+' AND TABLE_NAME = '+quotedStr('pemilih_'+kodes);
    qry9.Active:=true;
    if(qry9.RecordCount >= 1) then begin
      //cari tps di desa tsb
      totaltps:=0;
      sql_tps := 'SELECT ID_TPS, NAMA FROM daftar_tps WHERE KODE_DESA = '+quotedStr(kodes)+' ORDER BY NAMA';
      qry1.Active:=false;
      qry1.SQL.Clear;
      qry1.SQL.Text := sql_tps;
      qry1.Active := true;
      qry1.First;
      while not qry1.Eof do begin
        idtps := qry1.FieldByName('ID_TPS').AsString;
        natps := qry1.FieldByName('NAMA').AsString;
        sql_pemilih := 'SELECT CACAT FROM pemilih_'+kodes+' WHERE STATUS2 = ''0'' AND CACAT <> ''0'' AND TPS = '+idtps;
        qry2.Active:=false;
        qry2.SQL.Clear;
        qry2.SQL.Text:=sql_pemilih;
        qry2.Active:=true;
        qry2.First;
        kel1:=0;
        kel2:=0;
        kel3:=0;
        kel4:=0;
        kel5:=0;
        kel6:=0;
        while not qry2.Eof do begin
          hasil := qry2.fieldByName('CACAT').AsString;
          if(hasil ='1') then
            kel1:=kel1+1
          else if(hasil ='2') then
            kel2:=kel2+1
          else if(hasil ='3') then
            kel3:=kel3+1
          else if(hasil ='4') then
            kel4:=kel4+1
          else if(hasil ='5') then
            kel5:=kel5+1
          else if(hasil ='6') then
            kel6:=kel6+1;
          qry2.Next;
        end;

        tkel1:=tkel1 + kel1;
        tkel2:=tkel2 + kel2;
        tkel3:=tkel3 + kel3;
        tkel4:=tkel4 + kel4;
        tkel5:=tkel5 + kel5;
        tkel6:=tkel6 + kel6;

        sql_insert := 'INSERT INTO rekap_pemilih_cacat VALUES('+quotedStr(inttostr(no))+','+quotedStr(takab)+','+quotedstr(takec)+','+quotedstr(tades)+','+quotedStr(natps)+','+quotedstr(inttostr(kel1))+','+quotedstr(inttostr(kel2))+','+quotedstr(inttostr(kel3))+','+quotedstr(inttostr(kel4))+','+quotedstr(inttostr(kel5))+','+quotedstr(inttostr(kel6))+')';
        qry3.Active:=false;
        qry3.SQL.Clear;
        qry3.SQL.Text:=sql_insert;
        qry3.execSQL;

        qry1.Next;
        no := no+1;
        totaltps:=totaltps+1;
      end;
      sql_insert := 'INSERT INTO rekap_pemilih_cacat VALUES('+quotedStr(inttostr(no))+','+quotedStr(takab)+','+quotedstr(takec)+','+quotedstr('TOTAL '+tades)+','+quotedStr(inttostr(totaltps))+','+quotedstr(inttostr(tkel1))+','+quotedstr(inttostr(tkel2))+','+quotedstr(inttostr(tkel3))+','+quotedstr(inttostr(tkel4))+','+quotedstr(inttostr(tkel5))+','+quotedstr(inttostr(tkel6))+')';
      qry3.Active:=false;
      qry3.SQL.Clear;
      qry3.SQL.Text:=sql_insert;
      qry3.execSQL;
      no:=no+1;
    end;
    qryWil.Next;
  end;
end;

procedure TfrmInformasi.threadCacatFinishAll(Sender: TObject);
begin
  qryCacat.Active:=false;
  qryCacat.SQL.Clear;
  qryCacat.SQL.Text:='SELECT * FROM rekap_pemilih_cacat';
  qryCacat.Active:=true;
  //dsKelUmur.DataSet:=qryKelUmur;

  cxGrid13.BeginUpdate;
  cxGridDBBandedTableView7.DataController.DataSource:=dsCacat;
  cxGrid13.EndUpdate;
end;

procedure TfrmInformasi.Button26Click(Sender: TObject);
begin
  try
    if openDlg.Execute then
      ExportGridToExcel(openDlg.FileName,cxGrid14,False);
  finally
    MessageDlg('Export Berhasil',mtConfirmation,[mbOk],0);
  end;
end;

procedure TfrmInformasi.Button25Click(Sender: TObject);
begin
  threadKK.ThreadDialog := threadDialogKK;
  threadKK.Execute(Self);
end;

procedure TfrmInformasi.threadKKFinishAll(Sender: TObject);
begin
  qryKK.Active:=false;
  qryKK.SQL.Clear;
  qryKK.SQL.Text:='SELECT * FROM rekap_kk';
  qryKK.Active:=true;
  //dsKelUmur.DataSet:=qryKelUmur;

  cxGrid14.BeginUpdate;
  cxGridDBBandedTableView8.DataController.DataSource:=dsKK;
  cxGrid14.EndUpdate;
end;

procedure TfrmInformasi.threadKKExecute(Sender: TObject; Params: Pointer);
var
  sql_wilayah, sql_cek, sql_tps, sql_pemilih, sql_create, sql_truncate, sql_insert:String;
  kodes, kokec, tades, takec, takab, cacat, idtps, natps,hasil : String;
  no, kk, tanpa, total, totaltps : integer;
  tkel1, tkel2, tkel3, tkel4, tkel5, tkel6: integer;
begin
  sql_create:= 'CREATE TABLE IF NOT EXISTS `rekap_kk` ('
  +'`no` int(11) NOT NULL,'
  +'`kabkot` char(40) NOT NULL,'
  +'`kec` char(40) NOT NULL,'
  +'`desa` char(40) NOT NULL,'
  +'`tps` char(40) NOT NULL,'
  +'`jml_kk` int(11) NOT NULL COMMENT ''jumlah KK'','
  +'`jml_tanpa` int(11) NOT NULL COMMENT ''tanpa KK'','
  +'`total` int(11) NOT NULL COMMENT ''total'''
  +') ENGINE=MyISAM DEFAULT CHARSET=latin1;';

  qryCreate.Active:=false;
  qryCreate.SQL.Clear;
  qryCreate.SQL.Text:= sql_create;
  qryCreate.ExecSQL;

  //truncate dulu
  sql_truncate:='TRUNCATE rekap_kk';
  qryCreate.Active:=false;
  qryCreate.SQL.Clear;
  qryCreate.SQL.Text:= sql_truncate;
  qryCreate.ExecSQL;

  //wilayah
  //cari kota
  sql_wilayah := 'SELECT a.KODE_DESA AS KODES, a.TAMPIL AS NDESA, b.KODE_KECAMATAN AS KOKEC, b.TAMPIL AS NKEC, c.TAMPIL AS NKABKOT FROM (desa a INNER JOIN kecamatan b ON a.KODE_KECAMATAN LIKE b.KODE_KECAMATAN)'+' INNER JOIN kabkot c ON c.KODE = a.KODE_KABKOT WHERE a.KODE_KABKOT LIKE '+quotedStr(skota);
  //showmessage(sqlAwal);
  no:=1;
  qryWil.active := false;
  qryWil.SQL.Clear;
  qryWil.SQL.Text := sql_wilayah;
  qryWil.Active := true;
  qryWil.First;
  while not qryWil.Eof do begin
    kokec := qryWil.FieldByName('KOKEC').AsString;
    kodes := qryWil.FieldByName('KODES').AsString;
    tades := qryWil.FieldByName('NDESA').AsString;
    takec := qryWil.FieldByName('NKEC').AsString;
    takab := qryWil.FieldByName('NKABKOT').AsString;

    tkel1:=0;
    tkel2:=0;
    tkel3:=0;
    tkel4:=0;
    tkel5:=0;
    tkel6:=0;

    qry9.active:=false;
    qry9.SQL.Clear;
    qry9.SQL.Text := 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '+quotedStr(con.Database)+' AND TABLE_NAME = '+quotedStr('pemilih_'+kodes);
    qry9.Active:=true;
    if(qry9.RecordCount >= 1) then begin
      //cari tps di desa tsb
      totaltps:=0;
      sql_tps := 'SELECT ID_TPS, NAMA FROM daftar_tps WHERE KODE_DESA = '+quotedStr(kodes)+' ORDER BY NAMA';
      qry1.Active:=false;
      qry1.SQL.Clear;
      qry1.SQL.Text := sql_tps;
      qry1.Active := true;
      qry1.First;
      while not qry1.Eof do begin
        idtps := qry1.FieldByName('ID_TPS').AsString;
        natps := qry1.FieldByName('NAMA').AsString;
        sql_pemilih := 'SELECT COUNT(DISTINCT(NKK)) AS HITUNG FROM pemilih_'+kodes+' WHERE STATUS2 = ''0'' AND TPS = '+idtps+' AND NKK <> ''''';
        qry2.Active:=false;
        qry2.SQL.Clear;
        qry2.SQL.Text:=sql_pemilih;
        qry2.Active:=true;
        qry2.First;

        kk := qry2.fieldByName('HITUNG').AsInteger;

        sql_pemilih := 'SELECT COUNT((NKK)) AS HITUNG FROM pemilih_'+kodes+' WHERE STATUS2 = ''0'' AND TPS = '+idtps+' AND NKK = ''''';
        qry2.Active:=false;
        qry2.SQL.Clear;
        qry2.SQL.Text:=sql_pemilih;
        qry2.Active:=true;
        qry2.First;

        tanpa := qry2.fieldByName('HITUNG').AsInteger;

        sql_pemilih := 'SELECT COUNT((ID_USER)) AS HITUNG FROM pemilih_'+kodes+' WHERE STATUS2 = ''0'' AND TPS = '+idtps+'';
        qry2.Active:=false;
        qry2.SQL.Clear;
        qry2.SQL.Text:=sql_pemilih;
        qry2.Active:=true;
        qry2.First;

        total := qry2.fieldByName('HITUNG').AsInteger;

        sql_insert := 'INSERT INTO rekap_kk VALUES('+quotedStr(inttostr(no))+','+quotedStr(takab)+','+quotedstr(takec)+','+quotedstr(tades)+','+quotedStr(natps)+','+quotedstr(inttostr(kk))+','+quotedstr(inttostr(tanpa))+','+quotedstr(inttostr(total))+')';
        qry3.Active:=false;
        qry3.SQL.Clear;
        qry3.SQL.Text:=sql_insert;
        qry3.execSQL;

        qry1.Next;
        no := no+1;
        totaltps:=totaltps+1;
      end;
      {sql_insert := 'INSERT INTO rekap_pemilih_cacat VALUES('+quotedStr(inttostr(no))+','+quotedStr(takab)+','+quotedstr(takec)+','+quotedstr('TOTAL '+tades)+','+quotedStr(inttostr(totaltps))+','+quotedstr(inttostr(tkel1))+','+quotedstr(inttostr(tkel2))+','+quotedstr(inttostr(tkel3))+','+quotedstr(inttostr(tkel4))+','+quotedstr(inttostr(tkel5))+','+quotedstr(inttostr(tkel6))+')';
      qry3.Active:=false;
      qry3.SQL.Clear;
      qry3.SQL.Text:=sql_insert;
      qry3.execSQL;
      no:=no+1;}
    end;
    qryWil.Next;
  end;
end;

procedure TfrmInformasi.Button28Click(Sender: TObject);
begin
  try
    if openDlg.Execute then
      ExportGridToExcel(openDlg.FileName,cxGrid15,False);
  finally
    MessageDlg('Export Berhasil',mtConfirmation,[mbOk],0);
  end;
end;

procedure TfrmInformasi.threadRRTFinishAll(Sender: TObject);
begin
  qryRRT.Active:=false;
  qryRRT.SQL.Clear;
  qryRRT.SQL.Text:='SELECT * FROM rekap_rrt';
  qryRRT.Active:=true;
  //dsKelUmur.DataSet:=qryKelUmur;

  cxGrid15.BeginUpdate;
  cxGridDBBandedTableView9.DataController.DataSource:=dsRRT;
  cxGrid15.EndUpdate;
end;

procedure TfrmInformasi.threadRRTExecute(Sender: TObject; Params: Pointer);
var
  sql_wilayah, sql_cek, sql_tps, sql_pemilih, sql_create, sql_truncate, sql_insert:String;
  kodes, kokec, tades, takec, takab, cacat, idtps, natps,hasil,rw,rt : String;
  no, jml_lk, jml_pr, total, totaltps : integer;
  tkel1, tkel2, tkel3, tkel4, tkel5, tkel6: integer;
begin
  sql_create:= 'CREATE TABLE IF NOT EXISTS `rekap_rrt` ('
  +'`no` int(11) NOT NULL,'
  +'`kabkot` char(40) NOT NULL,'
  +'`kec` char(40) NOT NULL,'
  +'`desa` char(40) NOT NULL,'
  +'`tps` char(40) NOT NULL,'
  +'`rt` char(3) NOT NULL COMMENT ''RT'','
  +'`rw` char(3) NOT NULL COMMENT ''RW'','
  +'`jml_lk` int(11) NOT NULL COMMENT ''jumlah laki2'','
  +'`jml_pr` int(11) NOT NULL COMMENT ''jumlah peremupan'','
  +'`total` int(11) NOT NULL COMMENT ''total'''
  +') ENGINE=MyISAM DEFAULT CHARSET=latin1;';

  qryCreate.Active:=false;
  qryCreate.SQL.Clear;
  qryCreate.SQL.Text:= sql_create;
  qryCreate.ExecSQL;

  //truncate dulu
  sql_truncate:='TRUNCATE rekap_rrt';
  qryCreate.Active:=false;
  qryCreate.SQL.Clear;
  qryCreate.SQL.Text:= sql_truncate;
  qryCreate.ExecSQL;

  //wilayah
  //cari kota
  sql_wilayah := 'SELECT a.KODE_DESA AS KODES, a.TAMPIL AS NDESA, b.KODE_KECAMATAN AS KOKEC, b.TAMPIL AS NKEC, c.TAMPIL AS NKABKOT FROM (desa a INNER JOIN kecamatan b ON a.KODE_KECAMATAN LIKE b.KODE_KECAMATAN)'+' INNER JOIN kabkot c ON c.KODE = a.KODE_KABKOT WHERE a.KODE_KABKOT LIKE '+quotedStr(skota);
  //showmessage(sqlAwal);
  no:=1;
  qryWil.active := false;
  qryWil.SQL.Clear;
  qryWil.SQL.Text := sql_wilayah;
  qryWil.Active := true;
  qryWil.First;
  while not qryWil.Eof do begin
    kokec := qryWil.FieldByName('KOKEC').AsString;
    kodes := qryWil.FieldByName('KODES').AsString;
    tades := qryWil.FieldByName('NDESA').AsString;
    takec := qryWil.FieldByName('NKEC').AsString;
    takab := qryWil.FieldByName('NKABKOT').AsString;

    tkel1:=0;
    tkel2:=0;
    tkel3:=0;
    tkel4:=0;
    tkel5:=0;
    tkel6:=0;

    qry9.active:=false;
    qry9.SQL.Clear;
    qry9.SQL.Text := 'SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '+quotedStr(con.Database)+' AND TABLE_NAME = '+quotedStr('pemilih_'+kodes);
    qry9.Active:=true;
    if(qry9.RecordCount >= 1) then begin
      //cari tps di desa tsb
      totaltps:=0;
      sql_tps := 'SELECT ID_TPS, NAMA FROM daftar_tps WHERE KODE_DESA = '+quotedStr(kodes)+' ORDER BY NAMA';
      qry1.Active:=false;
      qry1.SQL.Clear;
      qry1.SQL.Text := sql_tps;
      qry1.Active := true;
      qry1.First;
      while not qry1.Eof do begin
        idtps := qry1.FieldByName('ID_TPS').AsString;
        natps := qry1.FieldByName('NAMA').AsString;
        sql_pemilih := 'SELECT DISTINCT(RW) AS RWS FROM pemilih_'+kodes+' WHERE STATUS2 = ''0'' AND TPS = '+idtps+' ORDER BY RW';
        qry2.Active:=false;
        qry2.SQL.Clear;
        qry2.SQL.Text:=sql_pemilih;
        qry2.Active:=true;
        qry2.First;
        while not qry2.Eof do begin
          rw:=qry2.fieldByName('RWS').AsString;

          sql_pemilih := 'SELECT DISTINCT(RT) AS RTS FROM pemilih_'+kodes+' WHERE STATUS2 = ''0'' AND TPS = '+idtps+' AND RW = '+quotedStr(rw)+' ORDER BY RT';
          qry4.Active:=false;
          qry4.SQL.Clear;
          qry4.SQL.Text:=sql_pemilih;
          qry4.Active:=true;
          qry4.First;
          while not qry4.Eof do begin
            rt:=qry4.fieldByName('RTS').AsString;

            sql_pemilih := 'SELECT KELAMIN FROM pemilih_'+kodes+' WHERE STATUS2 = ''0'' AND TPS = '+idtps+' AND RW = '+quotedStr(rw)+' AND RT = '+quotedStr(rt)+'';
            qry5.Active:=false;
            qry5.SQL.Clear;
            qry5.SQL.Text:=sql_pemilih;
            qry5.Active:=true;
            qry5.First;
            jml_lk:=0;
            jml_pr:=0;
            total:=0;
            while not qry5.Eof do begin
              if(qry5.FieldByName('KELAMIN').AsString = 'L') then
                jml_lk := jml_lk+1
              else if(qry5.FieldByName('KELAMIN').AsString = 'P') then
                jml_pr := jml_pr+1;

              total:=total+1;
              qry5.Next;
            end;

            sql_insert := 'INSERT INTO rekap_rrt VALUES('+quotedStr(inttostr(no))+','+quotedStr(takab)+','+quotedstr(takec)+','+quotedstr(tades)+','+quotedStr(natps)+','+quotedStr(rt)+','+quotedStr(rw)+','+quotedstr(inttostr(jml_lk))+','+quotedstr(inttostr(jml_pr))+','+quotedstr(inttostr(total))+')';
            qry3.Active:=false;
            qry3.SQL.Clear;
            qry3.SQL.Text:=sql_insert;
            qry3.execSQL;

            no := no+1;
            qry4.Next;
          end;
          qry2.Next;
        end;
        qry1.Next;
        totaltps:=totaltps+1;
      end;
      {sql_insert := 'INSERT INTO rekap_pemilih_cacat VALUES('+quotedStr(inttostr(no))+','+quotedStr(takab)+','+quotedstr(takec)+','+quotedstr('TOTAL '+tades)+','+quotedStr(inttostr(totaltps))+','+quotedstr(inttostr(tkel1))+','+quotedstr(inttostr(tkel2))+','+quotedstr(inttostr(tkel3))+','+quotedstr(inttostr(tkel4))+','+quotedstr(inttostr(tkel5))+','+quotedstr(inttostr(tkel6))+')';
      qry3.Active:=false;
      qry3.SQL.Clear;
      qry3.SQL.Text:=sql_insert;
      qry3.execSQL;
      no:=no+1;}
    end;
    qryWil.Next;
  end;

end;

procedure TfrmInformasi.Button27Click(Sender: TObject);
begin
  threadRRT.ThreadDialog := threadDialogRRT;
  threadRRT.Execute(Self);
end;

procedure TfrmInformasi.ThreadTbExecute(Sender: TObject; Params: Pointer);
var
  qryX:TZQuery;
  qry,kel,qrycreate, sql_ins, kabkot, kec, des, kecsblm:string;
  jtps, jp, jl, jpr, jst, jbt, tps, p, l, pr, st, bt, jdesa: integer;
begin
  jdesa := 0;
  jtps := 0;
  jp := 0;
  jl := 0;
  jpr := 0;
  jst := 0;
  jbt := 0;
  
  cxGridDBBandedTableView10.Bands[0].Caption:='REKAP DATA PEMILIH TAMBAHAN '+cbKabKot1.Text;
  qryCek.Active:=False;
  qryCek.SQL.Clear;
  qryCek.SQL.Add('DROP TABLE IF EXISTS rekap_data_pemilih_tb');
  qryCek.ExecSQL;

  kecsblm:='';
  kec:='';

  qrycreate := 'CREATE TABLE IF NOT EXISTS `rekap_data_pemilih_tb` ('
  +'`id` int(11) NOT NULL AUTO_INCREMENT,'
  +'`kabkot` varchar(50) DEFAULT NULL,'
  +'`kecamatan` varchar(50) DEFAULT NULL,'
  +'`kelurahan` varchar(50) DEFAULT NULL,'
  +'`jml_tps` int(11) DEFAULT NULL,'
  +'`pemilih` int(11) DEFAULT NULL,'
  +'`sudah` int(11) DEFAULT NULL,'
  +'`belum` int(11) DEFAULT NULL,'
  +'`laki` int(11) DEFAULT NULL,'
  +'`perempuan` int(11) DEFAULT NULL,'
  +'PRIMARY KEY (`id`)'
  +') ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;';

  qryCek.Active:=False;
  qryCek.SQL.Clear;
  qryCek.SQL.Add(qrycreate);
  qryCek.ExecSQL;

  qryBantu.Active:=False;
  qryBantu.SQL.Clear;
  qryBantu.SQL.Add('SELECT * FROM desa WHERE LEFT(KODE_DESA,2)='+QuotedStr(kdKab)+' GROUP BY KODE_DESA');
//  qryBantu.SQL.Add('SELECT * FROM daftar_tps WHERE LEFT(KODE_DESA,2)='+QuotedStr(kdKab)+' GROUP BY KODE_DESA');
  qryBantu.Active:=True;
  with qryBantu do
  begin
    while not Eof do
    begin
      kel:=fieldByName('KODE_DESA').AsString;
//      ShowMessage(kel);
      qryX:=TZQuery.Create(Self);
      qryX.Connection:=con;
      qryX.Active:=False;
      qryX.SQL.Clear;
      qryX.SQL.Add(('SELECT * FROM information_schema.`TABLES` WHERE table_schema='+QuotedStr(con.Database)+' AND TABLE_NAME='+QuotedStr('pemilih_'+kel)));
      qryX.Active:=True;
//      ShowMessage(IntToStr(qryX.RecordCount));

      if qryX.RecordCount=1 then
      begin
        qry:='SELECT null,kabkot.TAMPIL AS kabkot,kecamatan.TAMPIL AS kecamatan,desa.TAMPIL AS kelurahan,(SELECT COUNT(*) FROM daftar_tps WHERE '+'KODE_DESA='+QuotedStr(kel)+') as tps,'
            +'(SELECT COUNT(ID_USER) FROM pemilih_'+kel+' WHERE STATUS2 = ''0'' AND (STATUS1 = ''1'' OR STATUS1 = ''3'')) AS total,(SELECT COUNT(ID_USER) FROM pemilih_'+kel+' WHERE TPS<>''0'' AND STATUS2 = ''0'' AND (STATUS1 = ''1'' OR STATUS1 = ''3'')) AS sudah,'
            +'(SELECT COUNT(ID_USER) FROM pemilih_'+kel+' WHERE TPS=''0'' AND STATUS2 = ''0'' AND (STATUS1 = ''1'' OR STATUS1 = ''3'')) AS belum, '
            +'(SELECT COUNT(ID_USER) FROM pemilih_'+kel+' WHERE KELAMIN=''L'' AND STATUS2 = ''0'' AND (STATUS1 = ''1'' OR STATUS1 = ''3'')) AS laki, '
            +'(SELECT COUNT(ID_USER) FROM pemilih_'+kel+' WHERE KELAMIN=''P'' AND STATUS2 = ''0'' AND (STATUS1 = ''1'' OR STATUS1 = ''3'')) AS perempuan '
            +'FROM kecamatan,desa,kabkot WHERE '
            +' LEFT(desa.KODE_DESA,5)=kecamatan.KODE_KECAMATAN AND desa.KODE_DESA='+QuotedStr(kel)
            +' AND kabkot.KODE=kecamatan.KODE_KABKOT GROUP BY desa.KODE_DESA';
        qryBantu2.Active:=False;
        qryBantu2.SQL.Clear;
        qryBantu2.SQL.Add(qry);
//        memo1.Lines.Add(qry);
        qryBantu2.Active:=true;
        qrybantu2.First;
        while not qryBantu2.Eof do begin
          kabkot := qryBantu2.fieldByName('kabkot').AsString;
          kec := qryBantu2.fieldByName('kecamatan').AsString;
          des := qryBantu2.fieldByName('kelurahan').AsString;
          tps := qryBantu2.fieldByName('tps').AsInteger;
          p := qryBantu2.fieldByName('total').AsInteger;
          l := qryBantu2.fieldByName('laki').AsInteger;
          pr := qryBantu2.fieldByName('perempuan').AsInteger;
          st := qryBantu2.fieldByName('sudah').AsInteger;
          bt := qryBantu2.fieldByName('belum').AsInteger;

          if(kec <> kecsblm) then begin
            if(kecsblm <> '') then begin
              //kecsblm := kec;
              sql_ins := 'INSERT INTO rekap_data_pemilih_tb VALUES(NULL, '+quotedStr(kabkot)+', '+quotedStr('TOTAL '+kecsblm)+', '+quotedStr(inttostr(jdesa))+', '+quotedStr(inttostr(jtps))+', '+quotedStr(inttostr(jp))+', '+quotedStr(inttostr(jst))+', '+quotedStr(inttostr(jbt))+', '+quotedStr(inttostr(jl))+', '+quotedStr(inttostr(jpr))+')';

              qry1.Active:=false;
              qry1.SQL.Clear;
              qry1.SQL.Text:=sql_ins;
              qry1.ExecSQL;

              jdesa := 0;
              jtps := 0;
              jp := 0;
              jl := 0;
              jpr := 0;
              jst := 0;
              jbt := 0;
              kecsblm := kec;
            end else kecsblm := kec;
          end;

          sql_ins := 'INSERT INTO rekap_data_pemilih_tb VALUES(NULL, '+quotedStr(kabkot)+', '+quotedStr(kec)+', '+quotedStr(des)+', '+quotedStr(inttostr(tps))+', '+quotedStr(inttostr(p))+', '+quotedStr(inttostr(st))+', '+quotedStr(inttostr(bt))+', '+quotedStr(inttostr(l))+', '+quotedStr(inttostr(pr))+')';

          qry1.Active:=false;
          qry1.SQL.Clear;
          qry1.SQL.Text:=sql_ins;
          qry1.ExecSQL;

          jdesa := jdesa+1;
          jtps := jtps+tps;
          jp := jp+p;
          jl := jl+l;
          jpr := jpr+pr;
          jst := jst+st;
          jbt := jbt+bt;

          qryBantu2.Next;
        end;
      end;
      qryX.Close;
      qryX.Destroy;
      Next;

    end;
    if(kec <> '') then begin
      kecsblm := kec;
      sql_ins := 'INSERT INTO rekap_data_pemilih_tb VALUES(NULL, '+quotedStr(kabkot)+', '+quotedStr('TOTAL '+kecsblm)+', '+quotedStr(inttostr(jdesa))+', '+quotedStr(inttostr(jtps))+', '+quotedStr(inttostr(jp))+', '+quotedStr(inttostr(jst))+', '+quotedStr(inttostr(jbt))+', '+quotedStr(inttostr(jl))+', '+quotedStr(inttostr(jpr))+')';

      qry1.Active:=false;
      qry1.SQL.Clear;
      qry1.SQL.Text:=sql_ins;
      qry1.ExecSQL;

      jdesa := 0;
      jtps := 0;
      jp := 0;
      jl := 0;
      jpr := 0;
      jst := 0;
      jbt := 0;
      kecsblm := kec;
    end;
  end;
  qryRekapDataPemilih.Active:=False;
  qryRekapDataPemilih.Active:=True;
end;

procedure TfrmInformasi.ThreadTbFinishAll(Sender: TObject);
begin
  qryTb.Active:=false;
  qryTb.SQL.Clear;
  qryTb.SQL.Text:='SELECT * FROM rekap_data_pemilih_tb';
  qryTb.Active:=true;
  //dsKelUmur.DataSet:=qryKelUmur;

  cxGrid16.BeginUpdate;
  cxGridDBBandedTableView10.DataController.DataSource:=dsTb;
  cxGrid16.EndUpdate;
end;

procedure TfrmInformasi.Button29Click(Sender: TObject);
begin
  threadTb.ThreadDialog := threadDialogTb;
  threadTb.Execute(Self);
end;

procedure TfrmInformasi.Button32Click(Sender: TObject);
begin
  try
    if openDlg.Execute then
      ExportGridToExcel(openDlg.FileName,cxGrid17,False);
  finally
    MessageDlg('Export Berhasil',mtConfirmation,[mbOk],0);
  end;
end;

procedure TfrmInformasi.Button30Click(Sender: TObject);
begin
  try
    if openDlg.Execute then
      ExportGridToExcel(openDlg.FileName,cxGrid16,False);
  finally
    MessageDlg('Export Berhasil',mtConfirmation,[mbOk],0);
  end;
end;

procedure TfrmInformasi.Button31Click(Sender: TObject);
begin
  threadTbTPS.ThreadDialog := threadDialogTbTPS;
  threadTbTPS.Execute(Self);
end;

procedure TfrmInformasi.ThreadTbTPSExecute(Sender: TObject;
  Params: Pointer);
var
  qryX:TZQuery;
  qry,kel,qry_insert,qry_create:string;
  qry_jumlah:string;
  kab,kec,des:string;
  kecprev, desprev, tplkec:string;
  namatps,idtps:string;
  jjmll, jjmlp, jjmls, jtps:integer;
  jjjmll, jjjmlp, jjjmls, jjtps, jdesa:integer;
  jmll, jmlp, jmls: integer;
  tnd:integer;
begin
  cxGridDBBandedTableView11.Bands[0].Caption:='REKAP DATA PEMILIH TAMBAHAN PER TPS '+cbKabkot2.Text;

  tplkec:='';
  desprev:='';
  kecprev:='';

  jjmll :=0;
  jjmlp :=0;
  jjmls :=0;
  jtps :=0;

  jjjmll :=0;
  jjjmlp :=0;
  jjjmls :=0;
  jjtps :=0;
  jdesa :=0;

  //buat tabel jika belum ada
  qry_create := 'CREATE TABLE IF NOT EXISTS `rekap_tps_tb` ('
  +'`id` int(11) NOT NULL AUTO_INCREMENT,'
  +'`kabkot` varchar(40) DEFAULT NULL,'
  +'`kecamatan` varchar(40) DEFAULT NULL,'
  +'`kelurahan` varchar(40) DEFAULT NULL,'
  +'`tps` varchar(10) DEFAULT NULL,'
  +'`jumlah` int(11) DEFAULT NULL,'
  +'`laki` int(11) DEFAULT NULL,'
  +'`perempuan` int(11) DEFAULT NULL,'
  +'PRIMARY KEY (`id`)'
  +') ENGINE=MyISAM AUTO_INCREMENT=772 DEFAULT CHARSET=latin1';
  qryCek.Active:=False;
  qryCek.SQL.Clear;
  qryCek.SQL.Add(qry_create);
  qryCek.ExecSQL;

  //hapus dari rekap_tps;
  qryCek.Active:=False;
  qryCek.SQL.Clear;
  qryCek.SQL.Add('DELETE FROM rekap_tps_tb');
  qryCek.ExecSQL;

  {qryBantu.Active:=False;
  qryBantu.SQL.Clear;
  qryBantu.SQL.Add('SELECT * FROM daftar_tps WHERE LEFT(KODE_DESA,2)='+QuotedStr(kdKab)+' GROUP BY KODE_DESA');
  qryBantu.Active:=True;
  with qryBantu do
  begin
    while not Eof do
    begin
      kel:=fieldByName('KODE_DESA').AsString;
//      ShowMessage(kel);
      qryX:=TZQuery.Create(Self);
      qryX.Connection:=con;
      qryX.Active:=False;
      qryX.SQL.Clear;
      qryX.SQL.Add(('SELECT * FROM information_schema.`TABLES` WHERE table_schema='+QuotedStr(con.Database)+' AND TABLE_NAME='+QuotedStr('pemilih_'+kel)));
      qryX.Active:=True;
//      ShowMessage(IntToStr(qryX.RecordCount));

      if qryX.RecordCount=1 then  //ada tabel
      qryx.Active:=false;
      qryx.SQL.text:= 'TRUNCATE rekap_tps';
      qryx.execSQL;
      begin}

      kab:='';
      kec:='';
      des:='';

      //cari kode desa

      //iterasi utk tiap tps
      qryBantu.Active:=False;
      qryBantu.SQL.Clear;
      qryBantu.SQL.Text:= 'SELECT * FROM daftar_tps WHERE KODE_DESA LIKE '+QuotedStr(kdKab+'%')+'ORDER BY KODE_DESA, NAMA ASC';
      qryBantu.Active:=True;
      qryBantu.First;
      while not qryBantu.Eof do begin
        //cari kabkot kecamatan kelurahan
        kel:=qryBantu.fieldByName('KODE_DESA').AsString;
        qry:='SELECT a.TAMPIL AS NDESA, b.TAMPIL AS NKEC, c.TAMPIL AS NKABKOT FROM (desa a INNER JOIN kecamatan b ON a.KODE_KECAMATAN LIKE b.KODE_KECAMATAN) INNER JOIN kabkot c ON c.KODE = a.KODE_KABKOT WHERE a.KODE_DESA LIKE '+quotedStr(kel);

        //dapet
        qryBantu2.Active:=False;
        qryBantu2.SQL.Clear;
        qryBantu2.sql.text:=qry;
        qryBantu2.Active:=true;
        qryBantu2.First;
        kab := qryBantu2.fieldbyname('NKABKOT').AsString;
        kec := qryBantu2.fieldbyname('NKEC').AsString;
        des := qryBantu2.fieldbyname('NDESA').AsString;
        qryBantu2.Close;

          if(des <> desprev) then begin
            if(desprev <> '') then begin

              if(kec <> kecprev) then
                tplKec := kecprev
              else
                tplkec := kec;

              qry_insert:='INSERT INTO rekap_tps_tb VALUES(NULL, '+quotedstr(kab)+', '+quotedstr(tplkec)+', '+quotedstr('TOTAL '+desprev)+', '+quotedstr(inttostr(jtps))+','+inttostr(jjmls)+','+inttostr(jjmll)+','+inttostr(jjmlp)+')';
              qryX:=TZQuery.Create(Self);
              qryX.Connection:=con;
              with qryX do begin
                active:=false;
                sql.clear;
                sql.Text:=qry_insert;
                execsql;
              end;

              jjjmll := jjjmll + jjmll;
              jjjmlp := jjjmlp + jjmlp;
              jjjmls := jjjmls + jjmls;
              jjtps := jjtps + jtps;
              jdesa := jdesa +1;

              jjmll :=0;
              jjmlp :=0;
              jjmls :=0;
              jtps :=0;

              desprev:=des;

            end else desprev:=des;
          end;
          if(kec <> kecprev) then begin
            if(kecprev <> '') then begin
              qry_insert:='INSERT INTO rekap_tps_tb VALUES(NULL, '+quotedstr(kab)+', '+quotedstr('TOTAL '+kecprev)+', '+quotedstr(inttostr(jdesa))+', '+quotedstr(inttostr(jjtps))+','+inttostr(jjjmls)+','+inttostr(jjjmll)+','+inttostr(jjjmlp)+')';
              qryX:=TZQuery.Create(Self);
              qryX.Connection:=con;
              with qryX do begin
                active:=false;
                sql.clear;
                sql.Text:=qry_insert;
                execsql;
              end;

              jjjmll :=0;
              jjjmlp :=0;
              jjjmls :=0;
              jjtps :=0;
              jdesa :=0;

              kecprev := kec;
            end else kecprev:=kec;
          end;

        //iterasi berdasarkan tps
       { qry:= 'SELECT ID_TPS, NAMA FROM daftar_tps WHERE KODE_DESA LIKE '+quotedstr(kel);
        with QryBantu2 do begin
          Active:=false;
          sql.Clear;
          sql.text:=qry;
          active:=true;
          first;
          while not eof do
          begin}
        idtps := QryBantu.fieldbyname('ID_TPS').asString;
        namatps := QryBantu.fieldbyname('NAMA').asString;
        //showmessage(namatps);
            //cari jumlah
        with qryBantuTps do begin
          qry_jumlah:='SELECT (SELECT COUNT(ID_USER) AS HITUNG FROM pemilih_'+kel+' WHERE TPS = '+idtps+' AND STATUS2 = ''0'' AND (STATUS1 = ''1'' OR STATUS1 = ''3'')),';
          qry_jumlah:=qry_jumlah+' (SELECT COUNT(ID_USER) AS HITUNG FROM pemilih_'+kel+' WHERE KELAMIN = '+QuotedStr('L')+' AND TPS = '+idtps+' AND STATUS2 = ''0'' AND (STATUS1 = ''1'' OR STATUS1 = ''3'')),';
          qry_jumlah:=qry_jumlah+' (SELECT COUNT(ID_USER) AS HITUNG FROM pemilih_'+kel+' WHERE KELAMIN = '+QuotedStr('P')+' AND TPS = '+idtps+' AND STATUS2 = ''0'' AND (STATUS1 = ''1'' OR STATUS1 = ''3''))';
          active:=false;
          sql.Clear;
          sql.Text:=qry_jumlah;
          tnd:=0;
          active:=true;
          jmls:=0;
          jmll:=0;
          jmlp:=0;
          while not eof do begin

                jmls := Fields[0].AsInteger;
                jmll := Fields[1].AsInteger;
                jmlp := Fields[2].AsInteger;

            next;
          end;

          jjmll:=jjmll+jmll;
          jjmlp:=jjmlp+jmlp;
          jjmls:=jjmls+jmls;
          jtps :=jtps+1;

          qry_insert:='INSERT INTO rekap_tps_tb VALUES(NULL, '+quotedstr(kab)+', '+quotedstr(kec)+', '+quotedstr(des)+', '+quotedstr(namatps)+','+inttostr(jmls)+','+inttostr(jmll)+','+inttostr(jmlp)+')';
          qryX:=TZQuery.Create(Self);
          qryX.Connection:=con;
          with qryX do begin
            active:=false;
            sql.clear;
            sql.Text:=qry_insert;
            execsql;
          end;

       end;
            {next;
          end;
        end; }

        {qry:='INSERT INTO rekap_tps SELECT null,kabkot.NAMA AS kabkot,kecamatan.NAMA AS kecamatan,desa.NAMA AS kelurahan,daftar_tps.NAMA,'
          +'(SELECT COUNT(*) FROM pemilih_'+kel+' WHERE TPS=daftar_tps.ID_TPS) AS total,'
          +'(SELECT COUNT(*) FROM pemilih_'+kel+' WHERE TPS=daftar_tps.ID_TPS AND KELAMIN="L") AS Laki,'
          +'(SELECT COUNT(*)FROM pemilih_'+kel+' WHERE TPS=daftar_tps.ID_TPS AND KELAMIN="P") AS perempuan '
          +'FROM daftar_tps,kecamatan,desa,kabkot WHERE daftar_tps.KODE_DESA=desa.KODE_DESA AND '
          +'LEFT(daftar_tps.KODE_DESA,5)=kecamatan.KODE_KECAMATAN AND LEFT(daftar_tps.KODE_DESA,2)='+QuotedStr(kdKab)
          +' AND kabkot.KODE=kecamatan.KODE_KABKOT';}
       { qryBantu2.Active:=False;
        qryBantu2.SQL.Clear;
        qryBantu2.SQL.Add(qry);
//        memo1.Lines.Add(qry);
        qryBantu2.ExecSQL;
      end;
      qryX.Close;
      qryX.Destroy;
      Next;
    end;
  end;  }
       qrybantu.Next;
    end;
    if(des <> '') then begin
              if(kec <> kecprev) then
                tplKec := kecprev
              else
                tplkec := kec;

              qry_insert:='INSERT INTO rekap_tps_tb VALUES(NULL, '+quotedstr(kab)+', '+quotedstr(tplkec)+', '+quotedstr('TOTAL '+desprev)+', '+quotedstr(inttostr(jtps))+','+inttostr(jjmls)+','+inttostr(jjmll)+','+inttostr(jjmlp)+')';
              qryX:=TZQuery.Create(Self);
              qryX.Connection:=con;
              with qryX do begin
                active:=false;
                sql.clear;
                sql.Text:=qry_insert;
                execsql;
              end;

              jjjmll := jjjmll + jjmll;
              jjjmlp := jjjmlp + jjmlp;
              jjjmls := jjjmls + jjmls;
              jjtps := jjtps + jtps;
              jdesa := jdesa +1;

              jjmll :=0;
              jjmlp :=0;
              jjmls :=0;
              jtps :=0;

              desprev:=des;

            end;
            if(kecprev <> '') then begin

              qry_insert:='INSERT INTO rekap_tps VALUES(NULL, '+quotedstr(kab)+', '+quotedstr('TOTAL '+kecprev)+', '+quotedstr(inttostr(jdesa))+', '+quotedstr(inttostr(jjtps))+','+inttostr(jjjmls)+','+inttostr(jjjmll)+','+inttostr(jjjmlp)+')';
              qryX:=TZQuery.Create(Self);
              qryX.Connection:=con;
              with qryX do begin
                active:=false;
                sql.clear;
                sql.Text:=qry_insert;
                execsql;
              end;

              jjjmll :=0;
              jjjmlp :=0;
              jjjmls :=0;
              jjtps :=0;
              jdesa :=0;

              kecprev := kec;

            end;
end;

procedure TfrmInformasi.ThreadTbTPSFinishAll(Sender: TObject);
begin
  qryTbTPS.Active:=false;
  qryTbTPS.SQL.Clear;
  qryTbTPS.SQL.Text:='SELECT * FROM rekap_tps_tb';
  qryTbTPS.Active:=true;
  //dsKelUmur.DataSet:=qryKelUmur;

  cxGrid17.BeginUpdate;
  cxGridDBBandedTableView11.DataController.DataSource:=dsTbTPS;
  cxGrid17.EndUpdate;
end;

end.

